Calculating with floatingpoints

Hi Forum

I have the feeling i haven't thought this through...

I want to get absolut numbers out of this equation:

(ge*(i/trans))+(geold*((trans-i)/trans))

For example: (2300*(10/500))+(2100*((500-10)/500))

The result is always 0 'zero'.

What do i do wrong?

You used integer arithmetic

How do i do this in floating-arithmetic?

AWOL:
You used integer arithmetic

How do i do this in float-arithmetic?

By not using integer arithmetic(2300.0*(10.0/500.0))+(2100.0*((500.0-10.0)/500.0)) (that’s overkill, but hopefully you get the idea)

AWOL:
By not using integer arithmetic(2300.0*(10.0/500.0))+(2100.0*((500.0-10.0)/500.0)) (that's overkill, but hopefully you get the idea)

Okay, but i don't work with the numbers printed out. So every variable in this calculation has to be a float variable!

Thanx - this worked!

GistA:
Okay, but i don’t work with the numbers printed out. So every variable in this calculation has to be a float variable!

No, usually only one has to be a floating-point value.

you could perhaps mostly if not completely solve the problem just by moving some parentheses

for integer math, as an example, 40000 * (3/500) is NOT the same as (40000*3)/500.

GistA:
So every variable in this calculation has to be a float variable!

If you want to calculate everything in floating point then you'll generally need at least one float variable per algebraic term.

So for example, given the code:

   int a=2,b=3,c=4,d=5,e=6,f=7;
   float g;
   g = a/b + c/d + e/f;

You'd need to change to either

   int a=2,b=3,c=4,d=5,e=6,f=7;
   float g;
   g = 1.0*a/b + 1.0*c/d + 1.0*e/f;

or

   int a=2,b=3,c=4,d=5,e=6,f=7;
   float g;
   g = float(a)/b + float(c)/d + float(e)/f;

The first option is easier to type, but it does kind of leave a question mark over whether or not the compiler inserts a redundant multiplication. To be honest I've never verified it, but I very highly doubt that the multiplication would actually occur (and not be optimized away by the compiler).

So for example in your code the following would work:

float(ge)i/trans + float(geold)(trans-i)/trans;

As would:

1.0gei/trans + 1.0geold(trans-i)/trans;

Thanx again for your helpful input! ;}