473,320 Members | 1,850 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,320 software developers and data experts.

Data conversion,a simple but strange problem,please help!

Hi,

We all know that the return value of Math.Log(8,2) is 3,but how about
(int)Math.Log(8,2)? On my machine,the return value of (int)Math.Log(8,2) is
strange enough! it's not 3 but 2 ! I've tested other values such as
(int)Math.Log(16,2),(int)Math.Log(4,2),(int)Math.L og(32,2)...et, they all
return the same value as their counterpart Math.Log() methods,only
(int)Math.Log(8,2) is incorrect,is this a .net bug ? I've also tested
Math.Floor(Math.Log(8,2)),its return value is same as (int)Math.Log(8,2),but
Math.Ceiling(Math.Log(8,2)) and int.Parse(Math.Log(8,2).ToString()) returns
the correct value of 3! Dose anyone know what caused this strange behavior?
thanks in advance!
Feb 25 '06 #1
2 1341

"Webdiyer" <we******@hotmail.com> wrote in message
news:OJ*************@TK2MSFTNGP11.phx.gbl...
Hi,

We all know that the return value of Math.Log(8,2) is 3,


No it's not. Math.Log(8,2) is a double precision floating point value
guaranteed only to be pretty darn close to 3.

It's like the add for the new Delta shaver:
It's not just close, it's Eplsilon-close!

In particular (3 - double.Epsilon) < Math.Log(8,2) < (3 + double.Epsilon).

When you convert it to an integral type, you should explicitly apply
whatever rounding behavior you want. Or else perform your arithmetic on
integers only.

David
Feb 25 '06 #2
Thank you for your quick reply,I've solved this problem by first converting
Math.Log(8,2) to decimal value and then converting this value to int,this
way the result is correct.
"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:OH****************@TK2MSFTNGP12.phx.gbl...

"Webdiyer" <we******@hotmail.com> wrote in message
news:OJ*************@TK2MSFTNGP11.phx.gbl...
Hi,

We all know that the return value of Math.Log(8,2) is 3,


No it's not. Math.Log(8,2) is a double precision floating point value
guaranteed only to be pretty darn close to 3.

It's like the add for the new Delta shaver:
It's not just close, it's Eplsilon-close!

In particular (3 - double.Epsilon) < Math.Log(8,2) < (3 +
double.Epsilon).

When you convert it to an integral type, you should explicitly apply
whatever rounding behavior you want. Or else perform your arithmetic on
integers only.

David

Mar 1 '06 #3

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

Similar topics

0
by: news.paradise.net.nz | last post by:
My database appears to be a little corrupted, so far it's running along 99% ok, but i am getting some strange results. Primarily i can't back it up because of the following error. Similar errors...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
1
by: shalini jain | last post by:
Hi, I am being faced with a strange problem... I wrote a code for displaying pages in HTML and hence was using HTML parser. Now i am using the same code but now parsing using XHTML that is i want...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.