Archived

This topic is now archived and is closed to further replies.

jonbell

Float Bug

Recommended Posts

jenova    122
indeterminate floatng point value.

google for a detailed description.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
blizzard999    268
quote:
Original post by jonbell
I have a strange bug, one of my vectors keeps getting its x,y,z set to -1.#INDO

What is #INDO ?


Probably your debugger (you are using debug version) is alerting you that you have not initialized the variables; the debug version usually fill memory/variable with deafult values.
It''s not a bug but it can be a problem if you use your vector without init.

Share this post


Link to post
Share on other sites
jonbell    100
The values are correct until some bug occurs and then they become -1.INDO

What can cause a valid floating point value to be set to this? Am i correct in thinking that it does not have a value of -1 rather its value is undefined?

Share this post


Link to post
Share on other sites
blizzard999    268
quote:
Original post by jonbell
The values are correct until some bug occurs and then they become -1.INDO

What can cause a valid floating point value to be set to this? Am i correct in thinking that it does not have a value of -1 rather its value is undefined?


See also division by zero!

Share this post


Link to post
Share on other sites
jonbell    100
No i don''t get a crash so its not div by 0. I am seeing the values in the debugger but it is very difficult to get a breakpoint when the values get screwed cause i can''t test for -1.#indo

Anyone know what this value means and what can cause it?

Share this post


Link to post
Share on other sites
jenova    122
did you even consider my reply?

it stands for indeterminate float value. it can be caused by various operations (i.e. infinity * infinity, 0 * infinity, etc). it means the result cannot be determined.

ieeefloat

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by jonbell
No i don''t get a crash so its not div by 0. I am seeing the values in the debugger but it is very difficult to get a breakpoint when the values get screwed cause i can''t test for -1.#indo

Anyone know what this value means and what can cause it?


Floating point divisions by zero do not crash (only integer divisions do), they yield either +infinity, -infinity or NaN (Not a Number), which is precisely what you got here : you made, somewhere, an operation that doesn''t make sense mathematically (for real numbers) such as 0/0, infinity/infinity, infinity-infinity, square root of a negative number, arc-cosine of a number not in [-1,1], etc.

Share this post


Link to post
Share on other sites
jenova    122
fruny: teach a man to fish....

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Kippesoep    892
Actually, floating point dbz''s raise an exception, but floating point exceptions are masked by default on MSVC and many other compilers. Borland products (notably Delphi and C-builder) don''t mask them and will break the program.

Share this post


Link to post
Share on other sites
CWizard    127
A possibility is that you are overwriting the float with garbage. If I am not mistaking, there are illegal bit combinations in IEEE floats, that might show like that.

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by jonbell
Can i test for this value in code? If so how?


Your platform may define an isnan() function, otherwise, try this (untested, but AFAIK should be correct)


bool isnan(float value)
{
struct IEEE_float
{
bool sign : 1;
unsigned char exponent : 8;
unsigned int mantissa : 23;
};

IEEE_float v = reinterpret_cast<IEEE_float>(value);

return !~v.exponent && v.mantissa;
}

bool isnan(double value)
{
struct IEEE_double
{
bool sign : 1;
unsigned char exponent : 11;
unsigned int mantissa_high : 20;
unsigned int mantissa_low : 32
};

IEEE_double v = reinterpret_cast<IEEE_double>(value);

return !~v.exponent && (v.mantissa_high || v.mantissa_low);
}


i.e. maximum exponent and non-zero mantissa.

"Indeterminate" itself has a value of 0xFFC00000 (32-bit) or 0xFFF8000000000000 (64-bit).

[edited by - Fruny on January 8, 2004 9:51:57 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The following compiles and runs on my Visual Studio. You can follow the MDSN links to learn more about floating point error functions.


#include <stdio.h>
#include <conio.h>
#include <float.h>

const char * GetFloatDescription(float f)
{
switch (_fpclass(f))
{
case _FPCLASS_SNAN: return "Signaling NaN";
case _FPCLASS_QNAN: return "Quiet NaN";
case _FPCLASS_NINF: return "Negative infinity (-INF)";
case _FPCLASS_NN: return "Negative normalized non-zero";
case _FPCLASS_ND: return "Negative denormalized";
case _FPCLASS_NZ: return "Negative zero (-0)";
case _FPCLASS_PZ: return "Positive 0 (+0)";
case _FPCLASS_PD: return "Positive denormalized";
case _FPCLASS_PN: return "Positive normalized non-zero";
case _FPCLASS_PINF: return "Positive infinity (+INF)";
default: return "Error - Unexpected value from _fpclass()";
};
};

int main()
{
float fInfinite, fInd1, fInd2;
float fZero = 0.0;

// Infinity
fInfinite = 7.0f / fZero;

// Indeterminite
fInd1 = 0.0f * fInfinite;

// Indeterminite
fInd2 = 0.0f / fZero;

// Check to see if it''s Infinite
if (!_finite(fInfinite))
printf("fInfinity is Infinite\n";

// Check To see if it is a "NaN" (Not a Number)
if (_isnan(fInd1))
printf("fInd1 is Not A Number.\n";

// Check To see if it is a "NaN" (Not a Number)
if (_isnan(fInd2))
printf("fInd2 is Not A Number.\n";

printf("\n";

printf("fZero is %s\n", GetFloatDescription(fZero));
printf("fInfinite is %s\n", GetFloatDescription(fInfinite));
printf("fInd1 is %s\n", GetFloatDescription(fInd1));
printf("fInd2 is %s\n", GetFloatDescription(fInd2));

getch();
return 0;
}

Share this post


Link to post
Share on other sites