nagaharikola
asked on
how to use floating point numbers in calclations with out using the float data type
Hi
I have to work with float values but the micro i am using does not support floating point operations.
for exmple
x=y+z;
z=0.255
y=1;
then x should be 1.255 but i am getting 1 as the out put.
how to use the float values with out using the float data type in code.
I have to work with float values but the micro i am using does not support floating point operations.
for exmple
x=y+z;
z=0.255
y=1;
then x should be 1.255 but i am getting 1 as the out put.
how to use the float values with out using the float data type in code.
ASKER
it supports interger operations.
I an expressuion a + b , b is coming as float value.
when i add a and b. the value of b is not retaied as float but taken as int .
I have to retain that value
I an expressuion a + b , b is coming as float value.
when i add a and b. the value of b is not retaied as float but taken as int .
I have to retain that value
How is it coming in as a float value if float operations are not supported?
What do you mean "b is coming as float value."? How do you get a 'float value' on an integer machine?
Anyway, a typical way to do that used to be to scale the input values to allow the smallest value needed. Like @ozo suggested, scaling the values lets it work with integer arithmetic. You have to remember to display the results correctly on the output by putting a decimal where it needs to be.
Anyway, a typical way to do that used to be to scale the input values to allow the smallest value needed. Like @ozo suggested, scaling the values lets it work with integer arithmetic. You have to remember to display the results correctly on the output by putting a decimal where it needs to be.
ASKER
please give example for scaling
It's in @ozo's first post above.
ASKER
It is there. but again i have to divide by scale value to get the decimal number
What do you mean by "get"?
Do you mean print? If so, what print operations does the micro you are using support?
Do you mean print? If so, what print operations does the micro you are using support?
ASKER
how to retain the value of x=(5/10) using fixed point arthimatic.
please explain
please explain
What do you mean by "retain"?
What do you want to do with the value?
sum = y+x;
product = y*x/scale;
What do you want to do with the value?
sum = y+x;
product = y*x/scale;
ASKER
i have expression
x= (int)((a+b)*(value *(a-b)/100))
here a+b is coming as interger
value *(a-b)/100) is decimal
so the second part is discarded as i am trypecasting to int
but i dont want that value to be discarded.
x= (int)((a+b)*(value *(a-b)/100))
here a+b is coming as interger
value *(a-b)/100) is decimal
so the second part is discarded as i am trypecasting to int
but i dont want that value to be discarded.
ASKER
can u provide the fixed math algorithm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
in case the result x is a percentage and should be a rounded to an integer like 5, 25 or 70 % you could use an equivalent algorithm:
instead of
x= (int)((a+b)*(value *(a-b)/100))
use
x=(((a+b)*value*(a-b)) + 50)/100
that will work as long as (a+b)*value*(a-b)+50 is not greater than maximum int. the +50 makes correct rounding, for example if the product of the 3 factors is 150 by adding 50 it would be 200 and divided by 100 is 2. so the decimal result of 1.5 would be correctly rounded to 2.
Sara
instead of
x= (int)((a+b)*(value *(a-b)/100))
use
x=(((a+b)*value*(a-b)) + 50)/100
that will work as long as (a+b)*value*(a-b)+50 is not greater than maximum int. the +50 makes correct rounding, for example if the product of the 3 factors is 150 by adding 50 it would be 200 and divided by 100 is 2. so the decimal result of 1.5 would be correctly rounded to 2.
Sara
If you are using GCC, there is support for software floating point emulation for most targets that do not have hardware FP. The option in GCC is -msoft-float.
Using scaled arithmetic works fine for a small number of variables with limited range and precision. Otherwise it may be difficult to make workable.
Using scaled arithmetic works fine for a small number of variables with limited range and precision. Otherwise it may be difficult to make workable.
If it supports integer operations in the appropriate range, you might use
z=255
y=1000
x=1255