Solved

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

Posted on 2012-03-19
15
334 Views
Last Modified: 2012-04-01
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.
0
Comment
Question by:nagaharikola
  • 6
  • 4
  • 2
  • +3
15 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37736719
What operations and types does the micro you are using support?

If it supports integer operations in the appropriate range, you might use
z=255
y=1000
x=1255
0
 

Author Comment

by:nagaharikola
ID: 37736874
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 37736900
How is it coming in as a float value if float operations are not supported?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 37736908
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.
0
 

Author Comment

by:nagaharikola
ID: 37736913
please give example  for scaling
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 37736917
It's in @ozo's first post above.
0
 

Author Comment

by:nagaharikola
ID: 37736958
It is there. but again i have to divide by scale value to get the decimal number
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
ID: 37736991
What do you mean by "get"?  
Do you mean print?  If so, what print operations does the micro you are using support?
0
 

Author Comment

by:nagaharikola
ID: 37737042
how to retain the value of x=(5/10) using fixed point arthimatic.
please explain
0
 
LVL 84

Expert Comment

by:ozo
ID: 37737072
What do you mean by "retain"?
What do you want to do with the value?
sum = y+x;
product = y*x/scale;
0
 

Author Comment

by:nagaharikola
ID: 37737151
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.
0
 

Author Comment

by:nagaharikola
ID: 37737303
can u provide the fixed math algorithm
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 37737437
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))

becomes

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,
Kent
0
 
LVL 32

Expert Comment

by:sarabande
ID: 37738078
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
0
 
LVL 8

Expert Comment

by:eager
ID: 37743316
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now