473,746 Members | 2,672 Online

Floating point problem ............... ......

Hi

variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

Thanks
--------
Hitendra Patel

Nov 17 '05 #1
7 1973
Floating point numbers are just approximations to the number that you
actually want. Whenever you use them, you're going to get rounding errors.
If you don't want rounding errors, use the Decimal structure - that's a
fixed point numeric data type.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
"Hiten" <hi********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi

variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

Thanks
--------
Hitendra Patel

Nov 17 '05 #2
You seem not to understand the concept of floating point numbers.
Jon Skeet has a good article explaining "why are the arithmetics wrong",
refer to http://www.yoda.arachsys.com/csharp/floatingpoint.html or any

"Hiten" <hi********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi

variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

Thanks
--------
Hitendra Patel

Nov 17 '05 #3
declare your variable as type 'decimal'

A good computer science textbook on numerical representations wouldn't hurt
either.

Your computing hardware can only accurately represent specific values. It
gets as close as it can while still allowing the 'radix' to float (hence the
name). This allows the same variable to represent very large numbers (1.234
* 10^13) and very small numbers (0.34 * 10^-14).

Decimal types doesn't try. The decimal type represents a fixed number of
digits of accuracy. If the number cannot be represented by that fixed width
radix (as in astronomy calculations or algebraic formulas), then decimal
or calculations where the rounding error is precalculated (such as
comparisons against a lookup table of some kind), then you are likely to be
much better off declaring decimal variables.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Hiten" <hi********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi

variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

Thanks
--------
Hitendra Patel

Nov 17 '05 #4
Tim Haughton <ti*********@gm ail.com> wrote:
Floating point numbers are just approximations to the number that you
actually want. Whenever you use them, you're going to get rounding errors.
If you don't want rounding errors, use the Decimal structure - that's a
fixed point numeric data type.

No, decimal is still floating point type - it's just a floating
*decimal* point rather than a floating *binary* point.

Floats/doubles are precise in the same way that decimals are - it's
just that the set of numbers which can be precisely represented is
different.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
Nov 17 '05 #5
it works with decimal i knew that there are problems with float &
double data type......

but to hold exact value with decimal with float,decimal we put f,m
after litral that is 100.99f & 100.99m but suppose i want some values
to be stored in variable and i want tha variable must hold exact value
as user enter

so there is any other way for it or i have to write my own logic or
class....

Nov 17 '05 #6
Nick Malik [Microsoft] <ni*******@hotm ail.nospam.com> wrote:
declare your variable as type 'decimal'

A good computer science textbook on numerical representations wouldn't hurt
either.

Your computing hardware can only accurately represent specific values. It
gets as close as it can while still allowing the 'radix' to float (hence the
name). This allows the same variable to represent very large numbers (1.234
* 10^13) and very small numbers (0.34 * 10^-14).

Decimal types doesn't try. The decimal type represents a fixed number of
digits of accuracy. If the number cannot be represented by that fixed width
radix (as in astronomy calculations or algebraic formulas), then decimal
or calculations where the rounding error is precalculated (such as
comparisons against a lookup table of some kind), then you are likely to be
much better off declaring decimal variables.

I'm afraid there's a bit of confusion here.

Firstly, the radix itself doesn't float - it's 2 for double/float, and
10 for decimal. The radix *point* (i.e. the value of the exponent)
floats - and that's true for both decimal and float/double - they're
all floating points.

However, the range of the exponent in decimal is much smaller than the
range of the exponent in float/double.

In both cases, until you reach the extremes, the accuracy is
effectively fixed - 28 or 29 decimal digits for decimal, 15 or 16
decimal digits for double, and 7 decimal digits for float.

The most important difference between decimal and float/double,
however, is the base (the radix) - as decimal's radix is 10, it can
precisely store numbers which can be precisely represented as decimals
(subject to number of decimal places and scale). Compare this with
float/double, neither of which can store even 0.1 precisely, as that
cannot be precisely represented as a binary number.

See http://www.pobox.com/~skeet/csharp/decimal.html and
http://www.pobox.com/~skeet/csharp/floatingpoint.html for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
Nov 17 '05 #7
Hiten <hi********@gma il.com> wrote:
it works with decimal i knew that there are problems with float &
double data type......

but to hold exact value with decimal with float,decimal we put f,m
after litral that is 100.99f & 100.99m but suppose i want some values
to be stored in variable and i want tha variable must hold exact value
as user enter

so there is any other way for it or i have to write my own logic or
class....

You'll need to use Decimal.Parse, basically.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet