473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

why Math.Log(8,2) is 2.9999999999999996 and not 3

Hello Gurus,
Here is a problem, where i am unable to reason HOW it happens? I wrote
a dll using math.log. This dll when used within application A works
fine, but not with application B. On debugging I was biting my nails,
when i saw result in immediate window.

?Math.Log(8,2)
2.9999999999999996

Currently i am not using it anymore, but like to know if anybody faced
such a situtation. I am aware there were no overrides being used in
any of those two executables.
Thanks
Wave...
Nov 16 '05 #1
11 2013

"Whidbey Wave" <in*********@yahoo.com> wrote in message news:77**************************@posting.google.c om...
Hello Gurus,
Here is a problem, where i am unable to reason HOW it happens? I wrote
a dll using math.log. This dll when used within application A works
fine, but not with application B. On debugging I was biting my nails,
when i saw result in immediate window.

?Math.Log(8,2)
2.9999999999999996

Currently i am not using it anymore, but like to know if anybody faced
such a situtation. I am aware there were no overrides being used in
any of those two executables.
Thanks
Wave...


Fairly frequently asked question, though usually in the form of:
"why is 21.9 + 3.1 not exactly equal to 25?"

The "problem" is how computers (*any* computer, it's not
specifically .net related or even microsoft related) handle
floating point numbers. You CAN'T get exact results using
floating points!

As an example: try to specify 1/3 *exactly* in a finite number
of decimals, so that multiplying it by 3 gives exactly 1.

Hans Kesting
Nov 16 '05 #2
I think he meant why is it happening on Application B when Application A shows the correct result, 3.
I'm guessing that you don't use Math.Log(8,2) in both places, but pass the variables as floats or doubles that although may seem equal in both places, have been processed slightly different before being sent to Math.Log.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

"Morten Wennevik" <Mo************@hotmail.com> wrote in message news:opr9xsacxpklbvpo@morten_x.edunord...
I think he meant why is it happening on Application B when Application A shows the correct result, 3.
I'm guessing that you don't use Math.Log(8,2) in both places, but pass the variables as floats or doubles that although may seem equal in both places, have been processed slightly different before being sent to Math.Log.
--
Happy coding!
Morten Wennevik [C# MVP]


Okay, you are right.
What I *should* have added is that the printed result can also
depend on the way that is done: a "print" of the result might
automatically be done in (say) six decimals, in which case
the "real" result of 2.999999999996 can be *displayed*
as "3". So the calculation can result in exactly the same value
(as it should, if the inputvalues are the same) but the displayed
results can differ, if done in different ways (I'm guessing that
Application A was printing the result somewhere. The
result in Application B was viewed in the debugger)

Hans Kesting
Nov 16 '05 #4

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:OP**************@TK2MSFTNGP11.phx.gbl...

"Whidbey Wave" <in*********@yahoo.com> wrote in message news:77**************************@posting.google.c om...
Hello Gurus,
Here is a problem, where i am unable to reason HOW it happens? I wrote
a dll using math.log. This dll when used within application A works
fine, but not with application B. On debugging I was biting my nails,
when i saw result in immediate window.

?Math.Log(8,2)
2.9999999999999996

Currently i am not using it anymore, but like to know if anybody faced
such a situtation. I am aware there were no overrides being used in
any of those two executables.
Thanks
Wave...


Fairly frequently asked question, though usually in the form of:
"why is 21.9 + 3.1 not exactly equal to 25?"

The "problem" is how computers (*any* computer, it's not
specifically .net related or even microsoft related) handle
floating point numbers. You CAN'T get exact results using
floating points!


0.5 + 0.25 = 0.75

What part of that can't be represented exactly in binary floating-point?

As an example: try to specify 1/3 *exactly* in a finite number
of decimals, so that multiplying it by 3 gives exactly 1.

Hans Kesting

Nov 16 '05 #5
See e.g.:

http://www.math.grin.edu/~stone/cour...EEE-reals.html

http://research.microsoft.com/~holla...ieeefloat.html

A quick google search on IEEE floating point will turn up lots more info.

If you need to represent exact numbers (e.g. financial calcs where you can't
just drop a penny here and there) use System.decimal, but note that it will
only work with numbers that you can write down exactly (e.g. 1/3 can't be
represented exactly in decimal or float).

Colin

"Stu Smith" <st*****@nospam-digita.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:OP**************@TK2MSFTNGP11.phx.gbl...

"Whidbey Wave" <in*********@yahoo.com> wrote in message

news:77**************************@posting.google.c om...
Hello Gurus,
Here is a problem, where i am unable to reason HOW it happens? I wrote
a dll using math.log. This dll when used within application A works
fine, but not with application B. On debugging I was biting my nails,
when i saw result in immediate window.

?Math.Log(8,2)
2.9999999999999996

Currently i am not using it anymore, but like to know if anybody faced
such a situtation. I am aware there were no overrides being used in
any of those two executables.
Thanks
Wave...


Fairly frequently asked question, though usually in the form of:
"why is 21.9 + 3.1 not exactly equal to 25?"

The "problem" is how computers (*any* computer, it's not
specifically .net related or even microsoft related) handle
floating point numbers. You CAN'T get exact results using
floating points!


0.5 + 0.25 = 0.75

What part of that can't be represented exactly in binary floating-point?

As an example: try to specify 1/3 *exactly* in a finite number
of decimals, so that multiplying it by 3 gives exactly 1.

Hans Kesting


Nov 16 '05 #6
Colin Young <x@nospam.com> wrote:
See e.g.:

http://www.math.grin.edu/~stone/cour...EEE-reals.html

http://research.microsoft.com/~holla...ieeefloat.html

A quick google search on IEEE floating point will turn up lots more info.

If you need to represent exact numbers (e.g. financial calcs where you can't
just drop a penny here and there) use System.decimal, but note that it will
only work with numbers that you can write down exactly (e.g. 1/3 can't be
represented exactly in decimal or float).


That doesn't answer Stu's question though. He's absolutely right -
calculations involving things like 0.5 and 0.75 may well be represented
exactly.

Note that decimal floating point doesn't fundamentally represent
numbers exactly in a way that binary floating point can't - it's just a
different base, so more numbers are exactly representible.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
I just assumed he had picked those numbers at random. It wasn't entirely
clear from his post if he was genuinely curious, or just being a smart-ass.
I figured the OP could use the links I provided since it seems that their
code isn't dealing with numbers that are guaranteed to be exactly
representable (as does the code most of us probably write).

Colin

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Colin Young <x@nospam.com> wrote:
See e.g.:

http://www.math.grin.edu/~stone/cour...EEE-reals.html

http://research.microsoft.com/~holla...ieeefloat.html

A quick google search on IEEE floating point will turn up lots more info.
If you need to represent exact numbers (e.g. financial calcs where you can't just drop a penny here and there) use System.decimal, but note that it will only work with numbers that you can write down exactly (e.g. 1/3 can't be represented exactly in decimal or float).


That doesn't answer Stu's question though. He's absolutely right -
calculations involving things like 0.5 and 0.75 may well be represented
exactly.

Note that decimal floating point doesn't fundamentally represent
numbers exactly in a way that binary floating point can't - it's just a
different base, so more numbers are exactly representible.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8

"Colin Young" <x@nospam.com> wrote in message
news:u9**************@TK2MSFTNGP10.phx.gbl...
I just assumed he had picked those numbers at random. It wasn't entirely
clear from his post if he was genuinely curious, or just being a smart-ass. I figured the OP could use the links I provided since it seems that their
code isn't dealing with numbers that are guaranteed to be exactly
representable (as does the code most of us probably write).
Apologies, it was smart-ass-ness. It was just that chap said you "CAN'T" get
exact results from floating point, which to me seems just as inaccurate as
expecting exact results every time. Some numbers can be represented exactly,
and some (most!) numbers can't be. Obviously all FP numbers are exact
numbers themselves; it's the FP operations which give mathematically inexact
results at certain (but not all) times.

I'll try to hold back a bit on the nit-picking next time,
Stu

Colin

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Colin Young <x@nospam.com> wrote:
See e.g.:

http://www.math.grin.edu/~stone/cour...EEE-reals.html

http://research.microsoft.com/~holla...ieeefloat.html

A quick google search on IEEE floating point will turn up lots more info.
If you need to represent exact numbers (e.g. financial calcs where you can't just drop a penny here and there) use System.decimal, but note that it will only work with numbers that you can write down exactly (e.g. 1/3 can't be represented exactly in decimal or float).


That doesn't answer Stu's question though. He's absolutely right -
calculations involving things like 0.5 and 0.75 may well be represented
exactly.

Note that decimal floating point doesn't fundamentally represent
numbers exactly in a way that binary floating point can't - it's just a
different base, so more numbers are exactly representible.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #9
Stu Smith <st*****@nospam-digita.com> wrote:
Apologies, it was smart-ass-ness. It was just that chap said you "CAN'T" get
exact results from floating point, which to me seems just as inaccurate as
expecting exact results every time. Some numbers can be represented exactly,
and some (most!) numbers can't be. Obviously all FP numbers are exact
numbers themselves; it's the FP operations which give mathematically inexact
results at certain (but not all) times.


How you regard FP numbers is an interesting point, actually. You can
either regard them as exact numbers which are within a certain distance
of the last calculation, or you can regard them as a range in and of
themselves. I've never been entirely certain which is the best way of
thinking of them - I suspect it depends on the situation.

My point about decimal vs double is an important one though - people
tend to think of decimals as "exact" and doubles as "rough" when
there's no such distinction in reality - they have different bases,
levels of precision and scale ranges, but they're fundamentally much
more similar than they're usually portrayed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
No apologies necessary. I didn't want the OP to get confused since the issue
you raised requires an understanding of FP representation that it was clear
the OP didn't have.

I'm just jealous I didn't think of doing the same thing (I'll lay the blame
on being chronically sleep-deprived these days).

Colin

"Stu Smith" <st*****@nospam-digita.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...

"Colin Young" <x@nospam.com> wrote in message
news:u9**************@TK2MSFTNGP10.phx.gbl...
I just assumed he had picked those numbers at random. It wasn't entirely
clear from his post if he was genuinely curious, or just being a smart-ass.
I figured the OP could use the links I provided since it seems that their code isn't dealing with numbers that are guaranteed to be exactly
representable (as does the code most of us probably write).


Apologies, it was smart-ass-ness. It was just that chap said you "CAN'T"

get exact results from floating point, which to me seems just as inaccurate as
expecting exact results every time. Some numbers can be represented exactly, and some (most!) numbers can't be. Obviously all FP numbers are exact
numbers themselves; it's the FP operations which give mathematically inexact results at certain (but not all) times.

I'll try to hold back a bit on the nit-picking next time,
Stu

Colin

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Colin Young <x@nospam.com> wrote:
> See e.g.:
>
> http://www.math.grin.edu/~stone/cour...EEE-reals.html
>
> http://research.microsoft.com/~holla...ieeefloat.html >
> A quick google search on IEEE floating point will turn up lots more

info.
>
> If you need to represent exact numbers (e.g. financial calcs where you
can't
> just drop a penny here and there) use System.decimal, but note that
it will
> only work with numbers that you can write down exactly (e.g. 1/3
can't be
> represented exactly in decimal or float).

That doesn't answer Stu's question though. He's absolutely right -
calculations involving things like 0.5 and 0.75 may well be

represented exactly.

Note that decimal floating point doesn't fundamentally represent
numbers exactly in a way that binary floating point can't - it's just a different base, so more numbers are exactly representible.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 16 '05 #11
Here's a decent article explaining numbers in computers in pretty simple
terms: http://www.sybase.com/detail?id=1012599. I'm including it for
completeness for interested observers, not that I think Mr. Smith or Mr.
Skeet need this explained to them (I have a naive hope that people having
trouble with floating point will do a google search first and find this
thread archived).

I've actually never really completely understood the whole decimal thing. I
blame it on my engineering education. As far as I'm concerned 1.24e4 +
9.3e-1 = 1.24e4, but just try telling an accountant that 93 cents can just
be ignored.

Colin

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Stu Smith <st*****@nospam-digita.com> wrote:
Apologies, it was smart-ass-ness. It was just that chap said you "CAN'T" get exact results from floating point, which to me seems just as inaccurate as expecting exact results every time. Some numbers can be represented exactly, and some (most!) numbers can't be. Obviously all FP numbers are exact
numbers themselves; it's the FP operations which give mathematically inexact results at certain (but not all) times.


How you regard FP numbers is an interesting point, actually. You can
either regard them as exact numbers which are within a certain distance
of the last calculation, or you can regard them as a range in and of
themselves. I've never been entirely certain which is the best way of
thinking of them - I suspect it depends on the situation.

My point about decimal vs double is an important one though - people
tend to think of decimals as "exact" and doubles as "rough" when
there's no such distinction in reality - they have different bases,
levels of precision and scale ranges, but they're fundamentally much
more similar than they're usually portrayed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Frank Millman | last post by:
Hi all I was helping my niece with her trigonometry homework last night. Her calculator's batteries were flat, so I thought I would use Python's math module to calculate sin, cos, and tan. I...
0
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** ...
1
by: limelight | last post by:
I have discovered a math error in the .NET framework's Log function. It returns incorrect results for varying powers of 2 that depend on whether the program is run from within the IDE or from the...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
7
by: bravesplace | last post by:
Hello, I am using the folling funtion to round a number to a single digit on my form: function round1(num) { return Math.round(num*1)/1 }
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
0
by: kirby.urner | last post by:
Cyber-curricula have a leveling aspect, as kids nearer Katrina's epicenter tune in and bliss out on 'Warriors of the Net' (why wait for stupid big dummy textbooks to catch up?). They feel more...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
15
by: bH | last post by:
Hi All, I have been looking at javascript drawing from this website : http://www.cwdjr.net/geometricDraw/pentagon_draw.html" and I am wondering why the author made it into two images : upper...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.