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

Really weird rounding

Hi all,

I Have run this test:

Private Sub test()
Dim d As Double
Dim f As Single
Dim output As String

d = 8888888888888.8887
f = 9999999988888.8887

output = "Original double is: " & d.ToString() & " Formated
Double is: " & FormatNumber(d, 2, TriState.True) & vbCrLf _
& "Original single is: " & f.ToString() & "Formated Single is: "
& FormatNumber(f, 2, TriState.True)

MsgBox(output)
End Sub
The result is this:

Original Double is: 8888888888888.8887 Formated Double is:
8.888.888.888.888,89
Original Single is: 1E+13 Formated Single is 10.000.000.000.000,00

Now cames the big question:
How come that 9999999988888.8887 was rounded to 10.000.000.000.000,00
(or 1E+13 in scientific notation)?

This test was run in VB.Net 2003, 1.1 Framework.

Thanks for any help.

Feb 15 '07 #1
10 2143
Alsmeirelles,

Not weird and thousands of time written in the dotNet newsgroup.

In dotNet is ISO bankers rounding used. Not the way as humans do it.

If you search for this in this and the C# newsgroup on Google you get maybe
more pages than the Phone book of by instance New York.

Cor
<al**********@gmail.comschreef in bericht
news:11**********************@j27g2000cwj.googlegr oups.com...
Hi all,

I Have run this test:

Private Sub test()
Dim d As Double
Dim f As Single
Dim output As String

d = 8888888888888.8887
f = 9999999988888.8887

output = "Original double is: " & d.ToString() & " Formated
Double is: " & FormatNumber(d, 2, TriState.True) & vbCrLf _
& "Original single is: " & f.ToString() & "Formated Single is: "
& FormatNumber(f, 2, TriState.True)

MsgBox(output)
End Sub
The result is this:

Original Double is: 8888888888888.8887 Formated Double is:
8.888.888.888.888,89
Original Single is: 1E+13 Formated Single is 10.000.000.000.000,00

Now cames the big question:
How come that 9999999988888.8887 was rounded to 10.000.000.000.000,00
(or 1E+13 in scientific notation)?

This test was run in VB.Net 2003, 1.1 Framework.

Thanks for any help.

Feb 15 '07 #2
Hello, alsmeirelles

I don't think this has anything to do with "rounding" per se. It is
normal behaviour for floating point numbers.

Suggested reading: http://docs.sun.com/source/806-3568/ncg_goldberg.html

Cheers,
Randy
al**********@gmail.com wrote:
Hi all,

I Have run this test:

Private Sub test()
Dim d As Double
Dim f As Single
Dim output As String

d = 8888888888888.8887
f = 9999999988888.8887

output = "Original double is: " & d.ToString() & " Formated
Double is: " & FormatNumber(d, 2, TriState.True) & vbCrLf _
& "Original single is: " & f.ToString() & "Formated Single is: "
& FormatNumber(f, 2, TriState.True)

MsgBox(output)
End Sub
The result is this:

Original Double is: 8888888888888.8887 Formated Double is:
8.888.888.888.888,89
Original Single is: 1E+13 Formated Single is 10.000.000.000.000,00

Now cames the big question:
How come that 9999999988888.8887 was rounded to 10.000.000.000.000,00
(or 1E+13 in scientific notation)?

This test was run in VB.Net 2003, 1.1 Framework.

Thanks for any help.
Feb 15 '07 #3


I undestand that VB.Net uses the bankers rounding, what was done with
the double variable is perfect.
My question is about the behavior of the single. See, the original
number was 9999999988888.8887 .

In no way it could have been converted to 10.000.000.000.000,00.

And, as R.MacDonald said, it's true floating-point numbers have
"strange" behaviors sometimes, basically due to the lack of
representability they have in binary bases. For example,
there's no way to express exactly 0.1 in binary. Although I haven't
read the suggested article yet (but I willl), I think that its main
subject is about this lack of representability and what happens to
the numbers when they are in the edge of an overflow (which is not the
case here). So, I consider that my question is a little more tricky
than "it's the bankers rounding that does this".

Of course, I know very little of the .Net framework, and I may be an
ignorant concerning floating-points, so, any help is welcome.

Regards,
Alsm

On Feb 15, 10:13 pm, "R. MacDonald" <sci...@NO-SP-AMcips.cawrote:
Hello, alsmeirelles

I don't think this has anything to do with "rounding" per se. It is
normal behaviour for floating point numbers.

Suggested reading:http://docs.sun.com/source/806-3568/ncg_goldberg.html

Cheers,
Randy

alsmeirel...@gmail.com wrote:
Hi all,
I Have run this test:
Private Sub test()
Dim d As Double
Dim f As Single
Dim output As String
d = 8888888888888.8887
f = 9999999988888.8887
output = "Original double is: " & d.ToString() & " Formated
Double is: " & FormatNumber(d, 2, TriState.True) & vbCrLf _
& "Original single is: " & f.ToString() & "Formated Single is: "
& FormatNumber(f, 2, TriState.True)
MsgBox(output)
End Sub
The result is this:
Original Double is: 8888888888888.8887 Formated Double is:
8.888.888.888.888,89
Original Single is: 1E+13 Formated Single is 10.000.000.000.000,00
Now cames the big question:
How come that 9999999988888.8887 was rounded to 10.000.000.000.000,00
(or 1E+13 in scientific notation)?
This test was run in VB.Net 2003, 1.1 Framework.
Thanks for any help.

Feb 15 '07 #4
If you do this:

Dim g as Double = 9999999988888.8887

and then output it, you get 9,999,999,988,888.89

Doubles are more accurate.

Francesco Balena says in his VB2005 book "you should use Double variables
instead of Single variables if you care about performance because they
don't require any conversion when stored in and read from the CPU
floating-point registers." Maybe that conversion accounts for or
contributes to the round-off error you are seeing.

Robin S.
--------------------------------
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:u5**************@TK2MSFTNGP06.phx.gbl...
Alsmeirelles,

Not weird and thousands of time written in the dotNet newsgroup.

In dotNet is ISO bankers rounding used. Not the way as humans do it.

If you search for this in this and the C# newsgroup on Google you get
maybe more pages than the Phone book of by instance New York.

Cor
<al**********@gmail.comschreef in bericht
news:11**********************@j27g2000cwj.googlegr oups.com...
>Hi all,

I Have run this test:

Private Sub test()
Dim d As Double
Dim f As Single
Dim output As String

d = 8888888888888.8887
f = 9999999988888.8887

output = "Original double is: " & d.ToString() & " Formated
Double is: " & FormatNumber(d, 2, TriState.True) & vbCrLf _
& "Original single is: " & f.ToString() & "Formated Single is: "
& FormatNumber(f, 2, TriState.True)

MsgBox(output)
End Sub
The result is this:

Original Double is: 8888888888888.8887 Formated Double is:
8.888.888.888.888,89
Original Single is: 1E+13 Formated Single is 10.000.000.000.000,00

Now cames the big question:
How come that 9999999988888.8887 was rounded to 10.000.000.000.000,00
(or 1E+13 in scientific notation)?

This test was run in VB.Net 2003, 1.1 Framework.

Thanks for any help.


Feb 16 '07 #5
Alsmeirelles,

Although it is a C# adept, have a look at the pages from Jon Skeet, he is
the one the most busy with this in these newsgroups.

http://www.yoda.arachsys.com/csharp/

Cor

<al**********@gmail.comschreef in bericht
news:11*********************@l53g2000cwa.googlegro ups.com...
>

I undestand that VB.Net uses the bankers rounding, what was done with
the double variable is perfect.
My question is about the behavior of the single. See, the original
number was 9999999988888.8887 .

In no way it could have been converted to 10.000.000.000.000,00.

And, as R.MacDonald said, it's true floating-point numbers have
"strange" behaviors sometimes, basically due to the lack of
representability they have in binary bases. For example,
there's no way to express exactly 0.1 in binary. Although I haven't
read the suggested article yet (but I willl), I think that its main
subject is about this lack of representability and what happens to
the numbers when they are in the edge of an overflow (which is not the
case here). So, I consider that my question is a little more tricky
than "it's the bankers rounding that does this".

Of course, I know very little of the .Net framework, and I may be an
ignorant concerning floating-points, so, any help is welcome.

Regards,
Alsm

On Feb 15, 10:13 pm, "R. MacDonald" <sci...@NO-SP-AMcips.cawrote:
>Hello, alsmeirelles

I don't think this has anything to do with "rounding" per se. It is
normal behaviour for floating point numbers.

Suggested reading:http://docs.sun.com/source/806-3568/ncg_goldberg.html

Cheers,
Randy

alsmeirel...@gmail.com wrote:
Hi all,
I Have run this test:
Private Sub test()
Dim d As Double
Dim f As Single
Dim output As String
d = 8888888888888.8887
f = 9999999988888.8887
output = "Original double is: " & d.ToString() & " Formated
Double is: " & FormatNumber(d, 2, TriState.True) & vbCrLf _
& "Original single is: " & f.ToString() & "Formated Single is: "
& FormatNumber(f, 2, TriState.True)
MsgBox(output)
End Sub
The result is this:
Original Double is: 8888888888888.8887 Formated Double is:
8.888.888.888.888,89
Original Single is: 1E+13 Formated Single is 10.000.000.000.000,00
Now cames the big question:
How come that 9999999988888.8887 was rounded to 10.000.000.000.000,00
(or 1E+13 in scientific notation)?
This test was run in VB.Net 2003, 1.1 Framework.
Thanks for any help.


Feb 16 '07 #6
<al**********@gmail.comha scritto nel messaggio
I undestand that VB.Net uses the bankers rounding, what was done with
the double variable is perfect.
My question is about the behavior of the single. See, the original
number was 9999999988888.8887 .

In no way it could have been converted to 10.000.000.000.000,00.
this is not a banking rounding.
This is due to the accurancy of a System.Single: over a certain amount it
looses the digit infos and itroduces the 10based-exponent.
Feb 16 '07 #7
On Feb 16, 8:47 am, "Fabio" <znt.fa...@virgilio.itwrote:
<alsmeirel...@gmail.comha scritto nel messaggio
I undestand that VB.Net uses the bankers rounding, what was done with
the double variable is perfect.
My question is about the behavior of the single. See, the original
number was 9999999988888.8887 .
In no way it could have been converted to 10.000.000.000.000,00.

this is not a banking rounding.
This is due to the accurancy of a System.Single: over a certain amount it
Yes, definitely itīs not the rounding. The max value of a single is
3.40282347E+38,
so, if Single looses accuracy beyond a certain point, does anyone know
where is this point?

Finally, if the accuracy loss is the case here, thatīs a bug or what? I
īm within the possible values range.
looses the digit infos and itroduces the 10based-exponent.
Once again, thanks for everyone that has contributed.

Feb 16 '07 #8
al**********@gmail.com wrote:
On Feb 16, 8:47 am, "Fabio" <znt.fa...@virgilio.itwrote:
><alsmeirel...@gmail.comha scritto nel messaggio
>>I undestand that VB.Net uses the bankers rounding, what was done
with the double variable is perfect.
My question is about the behavior of the single. See, the original
number was 9999999988888.8887 .
>>In no way it could have been converted to 10.000.000.000.000,00.

this is not a banking rounding.
This is due to the accurancy of a System.Single: over a certain
amount it

Yes, definitely itīs not the rounding. The max value of a single is
3.40282347E+38,
so, if Single looses accuracy beyond a certain point, does anyone know
where is this point?

Finally, if the accuracy loss is the case here, thatīs a bug or what?
I īm within the possible values range.
It isn't accuracy, it's precision:
http://en.wikipedia.org/wiki/Accuracy_and_precision

You should also be aware of the concept of significant figures:
http://en.wikipedia.org/wiki/Significant_figures

And before you wonder about numbers like 0.1 (base 10) not being represented
exactly in the binary system (it's been done to death in many newsgroups),
remember that it's a recurring "decimal" in binary, the same as 1/3 is a
recurring decimal in base 10, and therefore cannot be represented exactly
with a limited number of digits for the representation. Books on numerical
analysis should deal with the matter rigorously.

The Decimal data type is another kettle of fish.

HTH

Andrew
Feb 16 '07 #9
"Andrew Morton" <ak*@in-press.co.uk.invalidha scritto nel messaggio

It isn't accuracy, it's precision:
http://en.wikipedia.org/wiki/Accuracy_and_precision
It still appears as accurancy to me :)
It depends on what are you talking about: a number of "high-values" or a
number of "closed high values".

Feb 16 '07 #10
al**********@gmail.com wrote:
On Feb 16, 8:47 am, "Fabio" <znt.fa...@virgilio.itwrote:
><alsmeirel...@gmail.comha scritto nel messaggio
>>I undestand that VB.Net uses the bankers rounding, what was done with
the double variable is perfect.
My question is about the behavior of the single. See, the original
number was 9999999988888.8887 .
In no way it could have been converted to 10.000.000.000.000,00.
this is not a banking rounding.
This is due to the accurancy of a System.Single: over a certain amount it

Yes, definitely itīs not the rounding. The max value of a single is
3.40282347E+38,
so, if Single looses accuracy beyond a certain point, does anyone know
where is this point?
As a floating point number is never accurate, it can't lose the accuracy.

If you mean at what point the limitation of precision is starting to
spill into whole numbers, it's when you are expressing numbers with more
than seven digits on the left of the decimal separator.
Finally, if the accuracy loss is the case here, thatīs a bug or what? I
īm within the possible values range.
Yes, but you are outside the range of the precision.
>looses the digit infos and itroduces the 10based-exponent.

Once again, thanks for everyone that has contributed.

--
Göran Andersson
_____
http://www.guffa.com
Feb 17 '07 #11

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

Similar topics

2
by: ai lian | last post by:
The code is as the following: { double limit= 0.02*33.00; double limit1= 0.06*11.00; double limit2= 0.03*22.00; double limit3= 0.01*66.00; limit=(floor(limit*100))/100;...
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
2
by: Jiri Nemec | last post by:
Hello all, I have got one table with rounding values, table contains prices and round types. id price_from price_to rounding 1 0 1500 0.1 2 1500 ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
3
by: aling | last post by:
Execute following T-SQL within Queary Analyzer of SQL Server 2000: ======================================= DECLARE @dTest DATETIME SET @dTest='2001-1-1 1:1:1:991' SELECT @dTest SET...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.