Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: while() with float ?
« on: February 18, 2006, 02:50:02 AM »
Quote

kas1e wrote:
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.


Work in integer and have a scale factor to convert the integer to your required float at any time. For instance

Code: [Select]

float scaleF = 0.01;
int i;
for(i=0; i<=100; i++) {
  float aa = scaleF*i;
  /* do whatever with a here */
}

This will ensure that on each iteration your value of a is as close to ideal as possible.

If you can do without actual floating point, try fixed point using integers as chaoslord suggests.
int p; // A