|
This problem involves floating point variables. When ever I add or subtract to a floating point variable I get strange numbers. For example if I add 0.5f to 5.0f, I get 5.499999999999 instead of 5.5. Here is an example:
float fExample = 5.0f;
fExample = fExample + 0.5f;
// fExample is now 5.499999999 !!!!
Is there something I am doing wrong, or is there a way of doing this without getting those ridiculous values.
Thanks in advance,
James Bird
|
|
|
|
|
Floating point is not entirely accurate. Doubles are more accurate than floats.
Why do you need 5.5 instead of 5.499999 ?
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Christian Graus wrote:
Why do you need 5.5 instead of 5.499999 ?
Thats what i was thinking...are they calculating the trajectory of a missle or something...? Maybe you shouldn't say anything..;P
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
If its just the display output that is the problem, you might want to have a look at this thread: here
|
|
|
|
|
Floating point is stored in base 2 and thus can have trouble representing numbers that look perfectly normal to us. Also, addition is inherently poor in floating point while multiplication works much better. If you can avoid repeatedly adding small numbers to a large number and instead perform a single add, the quality of the floating point number is much better.
For example:
float a = 1000;
for (int i = 0; i < 10000; i++)
{
a += 0.5;
}
float a = 1000;
for (int i = 0; i < 10000; i++)
{
float b = a + (float) i * 0.5;
}
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|
|
I know what a heap is, I know what a stack is but how do they relate to each other? I'm trying to reach a better understanding of memory management and not being able to put them in context is really throwing me.I know the question is a bit vague but so is my understanding at this point.
To overcome everything is to reach an invisible height where users cease to exist
|
|
|
|
|
Stack works on a push pop concept, heap is random.
stack is used for temporary static variables, heap is used to create dynamic variables.
Thats why you get mem leaks with:
Blah* blah = new Blah;
if no delete follows
but never with
Blah blah;
whatever is pushed onto the stack at startup is removed automatically at the end of the current scope, block, function thingy.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
HockeyDude wrote:
stack is used for temporary static variables, heap is used to create dynamic variables.
Thank you that clarifies everything!
To overcome everything is to reach an invisible height where users cease to exist
|
|
|
|
|
No worries be happy!
*dances like Harry Bellafonte*
Daaaaaaaaao
DaaaaaaaaaOh..
DB developer and me want to go home.
Left key, right key, left key type!!!
It's almost daylight here, so i'm gonna quit singing...
Nite'
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
HockeyDude wrote:
current scope, block, function thingy
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|
|
it 5:00 in the morning here Nish I'm at a loss for words.
Later brutha!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
HockeyDude wrote:
it 5:00 in the morning here Nish I'm at a loss for words
Aaaah
A fellow nocturnal creature.
HockeyDude wrote:
Later brutha!
Yup. later!
Nish
Nish was here, now Nish has gone;
He left his soul, to turn you on;
Those who knew Nish, knew him well;
Those who didn't, can go to hell.
I like to on the Code Project
Sonork ID 100.9786 voidmain
www.busterboy.org
|
|
|
|
|
Nish [BusterBoy] wrote:
A fellow nocturnal creature.
Yup like a racoon.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
Let's see if I can shed some more light on the subject.
There are basically two locations where memory can be allocated in C++: on the stack or on the heap.
The stack is used by the compiler mostly to allocate local variables whose size is well known at compile time. Every time you define a local variable (inside a function block), the compiler reserves a block of bytes for the size taken up by the variable. This is a very quick operation because all the compiler does is assign the address to the variable and move its "stack pointer" down by the size of the variable. This is called pushing the stack. When the variable goes out of scope the compiler does the opposite -- it pops the stack -- and the memory is reclaimed. Thus, memory allocated on the stack is automatically reclaimed by the compiler, and it's all very quick because it's all done in one location -- the end of the stack, if you will. The stack is also used whenever a function is called to hold its return address and its parameters (if any).
The heap is quite different. It's typically used to allocate variables that you plan to keep longer than duration of a function block, or variables whose size is not known until run-time (such as dynamic arrays). In C++, memory is taken from the heap via the new operator (or via the global C function "malloc"). When this happens, the heap manager looks for a block of memory large enough to hold the contents of your variable and returns the beginning address of that block. This block of memory stays allocated until your program calls the delete operator on that memory (or "free" if malloc was used) or until the program ends. By the way, allocating memory on the heap is much slower than allocating from the stack, so the stack should be used if the variable will not be needed outside the block where it was allocated and its size is known at compile-time. In addition, since the stack's memory is automatically reclaimed, it requires less housekeeping on the part of the programmer (to prevent memory leaks). However, the stack should ONLY be used when the variable will not be referenced outside the block where it was allocated, otherwise you'll be left with an invalid memory address which when used will cause the program to crash.
I hope to have cleared things up.
Regards,
Alvaro
Behind a beautiful woman there's usually a guy who just couldn't wait to get rid of her.
|
|
|
|
|
How can I do the following, but correctly?
LPSTR fileName = "data.dat";
class MyClass{
MyClass(){ CFile myFile(fileName, BLAH); }
~MyClass(){}
};
#define AfxSetFileName(file) (fileName = file)
I don't think I'm missing anything. Anyways i'm getting a linker error fileName already defined. Somebody wanna enlighten me and explain why...?
It's all in the one header file, if this makes a difference.
Also how do I prevent name collisions with third party libs using namespace on the global variable...?
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
You should declare your global variables in a .cpp file, and redeclare that variable in the header file with 'extern'. Because when you include the header into a second file, the variable gets redeclared and then you get a linker error, duplicate definition.
example:
.cpp file -
int g_Index = 0;
.h file -
extern int g_Index;
|
|
|
|
|
I can't put it in the cpp. There is none. It would defeat the purpose of my needs.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
HockeyDude wrote:
Also how do I prevent name collisions with third party libs using namespace on the global variable...?
The other half has been answered. To do it in a namespace, just decalre one
namespace MyNamespace
{
extern int g_int;
}
and in the .cpp declare it
int MyNamespace::g_int;
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
There is no CPP file...
The class is debug only so it's construction is wrapped by a define, so it go bye bye when release built.
It's instantiated many times over but can't afford to use static members, so i figured globals perfect.
Only i'm getting these damn lnk2002 errors and they drive me BALD! *pulls hair or whats left*
This is why i need a namespace and plan on using Afx global for changing the global variable.
Adios! (Spelling)
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
HockeyDude wrote:
There is no CPP file...
Then make one.
HockeyDude wrote:
Only i'm getting these damn lnk2002 errors and they drive me BALD! *pulls hair or whats left*
The answer you've been given is the only possibility I am aware of to solve your problem. Your variable needs to be decalred only once, and included everywhere you need it. This is what extern is for.
What is Afx global ? To change the variable you need only access it directly, through a namespace or not.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
I'm trying in a very non OOD sorta way to do something using OOD ideoligies(spelling).
There can't be a cpp the class has no implementation except for construction and destruction. Which for the sake of clarity and possibly speed are inlined.
Both use the same variable that will not change througout progam execution. They will however go through construction an destruction many times. I dunno...i see the problem now, but don't see any solution. I supose i could use a cpp for only a single var, but that wouldn't make sense over using a fixed value instead.
Thanx again!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
How are you able to build a project with no .cpp file?
Sprudling
|
|
|
|
|
Cuz there is a cpp, but it's not part of the header file i'm working on.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
inlining is only a suggestion to the compiler - it's just as likely the compiler will inline anyway, or not inline no matter what you say.
If the variable doesn't change then this is just stupid - make it a constant. Otherwise, there is no possible reason not to include a .cpp file for the sake of declaring the variable.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Nope, there are perfectly valid reasons why you want to do this. One of them is to create routine libraries without .LIB file.
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|