Welcome, Guest. Please login or register.

Author Topic: while() with float ?  (Read 3044 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline kas1eTopic starter

while() with float ?
« on: February 17, 2006, 11:33:09 AM »
hi all. here is question:

#include

main()
{
  float aa=0.0f;

  while(aa<1)
  {
     aa=aa+0.01;
     printf("%f\n",aa);
  }

}  

so, for sasc after 83.000 i have 83.9999 , for vbcc after 58.000 i have 58.9999. in other words, this float value added not exactly per 0.01. So, how i can anyway use float value but which will works exactly as i need ? Becouse tons of loops buggy over it .. i can of course do if > or if < , but i want to use !=aa.

any help ? thanks.
 

Offline kas1eTopic starter

Re: while() with float ?
« Reply #1 on: February 17, 2006, 12:23:11 PM »
Piru, thanks for help, will use fabs().
btw, what will be better (for speed), your example, or this:
while (fabs(a - 1) > 0.001) ?
 

Offline kas1eTopic starter

Re: while() with float ?
« Reply #2 on: February 17, 2006, 02:04:54 PM »
hm, btw. with while (aa<1) i have on exit from while : 1.009999 (so, not exactly 1.00) but with fabs() i have 0.99999 (so, not exactly 1.00 agayn).

if both case i will have problems, i need exactly 1.00000 as last think. it is possible ?

becouse with fabs, of without, i need to do somethink after while for 1.0000 rezult.
 

Offline kas1eTopic starter

Re: while() with float ?
« Reply #3 on: February 17, 2006, 04:03:35 PM »
thanks Cymric, Piru. Really good answers :)