Page 1 of 1

PostPosted: Wed Dec 11, 2002 2:29 pm
by gustave22
main()
{
float valeur1,valeur2,valeur;
valeur1=10000.02;
valeur2=10000.01;

showconsole();
valeur= valeur1-valeur2;

putsl("valeur : "+valeur);
}

/* puts on screen
valeur : 1.0000000000218e-002 */

Don't understand 218 ???

PostPosted: Thu Dec 12, 2002 12:07 pm
by guy
Floating point numbers cannot hold precise values (except for a limited set of numbers). Any result of a floating point calculation will end up as the closest floating point value that can be represented. Neither 10000.02 nor 100000.01 can be represented exactly as floating point values. The result of subtracting the two floating point values closest to 10000.02 and 10000.01 may not even be the closest floating point value to 0.01

If you wish to manipulate fixed point values I recommend that you use integers with a fixed assumed decimal exponent. ie:

valeur1=1000002 // Decimal exponent is -2
valeur2=1000001 // Decimal exponent is -2
valeur=valeur1-valeur2 // Exponent remains -2 for add/subtract
putsl("valeur * 100 : " + valeur) ;




Guy
[url]mailto:pcform@pcform.net[/url]
http://www.pcform.net
PocketC CE API interface: http://www.networkdynamics.net/PCForm.html#library
PCForm and CE API forum: http://www.networkdynamics.net/forum