C++ Math-Failure? modfl == mudfl?
-
Hello, I wonder about the behaviour of the modfl-function that splits a double into it´s integer and fractional part, and beside this why double values seem to have different values in different C++ IDEs. Here is my code: long double dVal; // Value to split into fractional and integer part long double dInt; // takes the integer part long double dFract; // takes the fractional part 1. dVal = 3333.3333; dFract = modfl(dVal,&dInt); //Result: // dInt = 3333.0000000000 // dFract = 0.33329999999978 2. dVal *= 10000; // should be 33333333.0 now? dFract = modfl(dVal,&dInt); // Result: // dInt = 33333332.0000000000 // dFract = 0.99999999627471 3. dVal = 33333333.0; dFract = modfl(dVal,&dInt); //Result: // dInt = 33333333.0000000000 // dFract = 0.0000000000 In Example 1, the dVal is stated to be 3333.3332999999998 when looking at it with the Visual Studio .NET debugger (watch), in Embedded C++ 4.0 it is 3333.33330000000, this is directly after assigning the constant value. Why is this? And especially: why does only the last example come up with correct values? Thanks... Martin
-
Hello, I wonder about the behaviour of the modfl-function that splits a double into it´s integer and fractional part, and beside this why double values seem to have different values in different C++ IDEs. Here is my code: long double dVal; // Value to split into fractional and integer part long double dInt; // takes the integer part long double dFract; // takes the fractional part 1. dVal = 3333.3333; dFract = modfl(dVal,&dInt); //Result: // dInt = 3333.0000000000 // dFract = 0.33329999999978 2. dVal *= 10000; // should be 33333333.0 now? dFract = modfl(dVal,&dInt); // Result: // dInt = 33333332.0000000000 // dFract = 0.99999999627471 3. dVal = 33333333.0; dFract = modfl(dVal,&dInt); //Result: // dInt = 33333333.0000000000 // dFract = 0.0000000000 In Example 1, the dVal is stated to be 3333.3332999999998 when looking at it with the Visual Studio .NET debugger (watch), in Embedded C++ 4.0 it is 3333.33330000000, this is directly after assigning the constant value. Why is this? And especially: why does only the last example come up with correct values? Thanks... Martin
this is normal floating point behaviour when applied to computers.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Hello, I wonder about the behaviour of the modfl-function that splits a double into it´s integer and fractional part, and beside this why double values seem to have different values in different C++ IDEs. Here is my code: long double dVal; // Value to split into fractional and integer part long double dInt; // takes the integer part long double dFract; // takes the fractional part 1. dVal = 3333.3333; dFract = modfl(dVal,&dInt); //Result: // dInt = 3333.0000000000 // dFract = 0.33329999999978 2. dVal *= 10000; // should be 33333333.0 now? dFract = modfl(dVal,&dInt); // Result: // dInt = 33333332.0000000000 // dFract = 0.99999999627471 3. dVal = 33333333.0; dFract = modfl(dVal,&dInt); //Result: // dInt = 33333333.0000000000 // dFract = 0.0000000000 In Example 1, the dVal is stated to be 3333.3332999999998 when looking at it with the Visual Studio .NET debugger (watch), in Embedded C++ 4.0 it is 3333.33330000000, this is directly after assigning the constant value. Why is this? And especially: why does only the last example come up with correct values? Thanks... Martin