problem with float ?

Discuss PocketC for CE (including Desktop Edition)

Postby gustave22 on Wed Dec 11, 2002 2:29 pm

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 ???
gustave22
 
Posts: 1
Joined: Wed Dec 11, 2002 1:13 pm

Postby guy on Thu Dec 12, 2002 12:07 pm

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
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
guy
 
Posts: 879
Joined: Thu Dec 07, 2000 8:58 am
Location: United Kingdom


Return to PocketC for CE

Who is online

Users browsing this forum: No registered users and 2 guests

cron