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! 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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....
| | |