• C

how to use floating point numbers in calclations with out using the float data type

I have to work with float  values but the micro i am using does not support floating point operations.
for exmple
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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What operations and types does the micro you are using support?

If it supports integer operations in the appropriate range, you might use
nagaharikolaAuthor Commented:
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
How is it coming in as a float value if float operations are not supported?
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

Dave BaldwinFixer of ProblemsCommented:
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.
nagaharikolaAuthor Commented:
please give example  for scaling
Dave BaldwinFixer of ProblemsCommented:
It's in @ozo's first post above.
nagaharikolaAuthor Commented:
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?
nagaharikolaAuthor Commented:
how to retain the value of x=(5/10) using fixed point arthimatic.
please explain
What do you mean by "retain"?
What do you want to do with the value?
sum = y+x;
product = y*x/scale;
nagaharikolaAuthor Commented:
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.
nagaharikolaAuthor Commented:
can u provide the fixed math algorithm
Kent OlsenDBACommented:
Hi nagaharikola,

It seems we have a bit of a language barrier.  Maybe another voice will help?

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

Because all of your variables are integers, all values, including the intermediate values will have an integer type.  When you divide two values, the result is an integer.  In your example above you divide by 100.  If the divisor is between 1 and 99, you get the same result, 0.  In effect, 1/100 = 99/100, for the purpose of this calculation as both result in integer 0.

To capture the fractional part of the calculation you need to determine how many decimal places you need.  It's probably beyond the scope of your project to use true floating point (variable precision), so decide how many digits to the right of the decimal that you'll need.  Are 2 enough?  With your equation that becomes the simplest.  Are 3 enough?  That resolution is 1/1000.

 x= (int) ((a+b)*(value *(a-b)/100))


2 decimal places --  x = ((a+b) * (value *(a-b)))
3 decimal places --  x = ((a+b) * 1000 * (value *(a-b)/100))

When you convert the value to a string, insert a decimal point 2 (or 3 places) from the right.

If you need the fractional part for future computations, just subtract it.  But remember that you're using integer data types so the value is 1000 times the fraction's true value.

// Compute value * 1000

  x = ((a+b) * 1000 * (value *(a-b)/100))

//  Compute fractional part

  frac = x % 1000;

//  Restore x to the correct scale

  x /= 1000;

Good Luck,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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))


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.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.