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

Paranoic about real number imprecisions

Hallo,

It is know issue that due to the fact that computer has to store the real
numbers in limited set of bytes, thus causing a minor imprecision from the
decimal value that likely was stored. I don't know, how dotnet framework
stored doubles, but it's certain, that if I store, say, 1.234567 in some
real variable, it is stored in memory like something close to
1.2345670000000000000003454786544 or
1.234566999999999999999999999999999924354324.

Due to this problem it may sometimes cause some unexpected results when
comparing the real numbers in "if" statement.

Some programming languages have built-in protection, that compares only
limited number of digits (less than declared capability of the type), when
asked to compare two real numbers, thus the garbage in the lesser decimal
fields goes unnoticed. I have been using an "ApproximatelyEqual" method, for
comparing two real numbers and providing precision:

input: A, B, precision
output: (abs(A-B) <= 10^(-precision))

Example:
A = 1.00000000000001
B = 1.00000000000007
precision = 13
abs(A-B) = 0.00000000000006
10^(-precision) = 0.0000000000001
abs(A-B) <= 10^(-precision) = true

precision = 14
10^(-precision) = 0.00000000000001
abs(A-B) <= 10^(-precision) = false

For a project I am working on, where there are done multiple operations with
real numbers, I wanted to be sure, where there are some traps I may fall in.
Is it safe to compare the real numbers without custom "ApproximatelyEqual"
method?

Thanks,

Pavils
Nov 16 '05 #1
10 2669
Why you don't use double?

"Pavils Jurjans" <pa****@mailbox.riga.lv> schrieb im Newsbeitrag
news:e8**************@TK2MSFTNGP10.phx.gbl...
Hallo,

It is know issue that due to the fact that computer has to store the real
numbers in limited set of bytes, thus causing a minor imprecision from the
decimal value that likely was stored. I don't know, how dotnet framework
stored doubles, but it's certain, that if I store, say, 1.234567 in some
real variable, it is stored in memory like something close to
1.2345670000000000000003454786544 or
1.234566999999999999999999999999999924354324.

Due to this problem it may sometimes cause some unexpected results when
comparing the real numbers in "if" statement.

Some programming languages have built-in protection, that compares only
limited number of digits (less than declared capability of the type), when
asked to compare two real numbers, thus the garbage in the lesser decimal
fields goes unnoticed. I have been using an "ApproximatelyEqual" method, for comparing two real numbers and providing precision:

input: A, B, precision
output: (abs(A-B) <= 10^(-precision))

Example:
A = 1.00000000000001
B = 1.00000000000007
precision = 13
abs(A-B) = 0.00000000000006
10^(-precision) = 0.0000000000001
abs(A-B) <= 10^(-precision) = true

precision = 14
10^(-precision) = 0.00000000000001
abs(A-B) <= 10^(-precision) = false

For a project I am working on, where there are done multiple operations with real numbers, I wanted to be sure, where there are some traps I may fall in. Is it safe to compare the real numbers without custom "ApproximatelyEqual"
method?

Thanks,

Pavils

Nov 16 '05 #2
For real good precicion use the decimal type. Is is slower than double and
uses more memory but it is much more suited for precise calculations.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Pavils Jurjans" <pa****@mailbox.riga.lv> schrieb im Newsbeitrag
news:e8**************@TK2MSFTNGP10.phx.gbl...
Hallo,

It is know issue that due to the fact that computer has to store the real
numbers in limited set of bytes, thus causing a minor imprecision from the
decimal value that likely was stored. I don't know, how dotnet framework
stored doubles, but it's certain, that if I store, say, 1.234567 in some
real variable, it is stored in memory like something close to
1.2345670000000000000003454786544 or
1.234566999999999999999999999999999924354324.

Due to this problem it may sometimes cause some unexpected results when
comparing the real numbers in "if" statement.

Some programming languages have built-in protection, that compares only
limited number of digits (less than declared capability of the type), when
asked to compare two real numbers, thus the garbage in the lesser decimal
fields goes unnoticed. I have been using an "ApproximatelyEqual" method, for comparing two real numbers and providing precision:

input: A, B, precision
output: (abs(A-B) <= 10^(-precision))

Example:
A = 1.00000000000001
B = 1.00000000000007
precision = 13
abs(A-B) = 0.00000000000006
10^(-precision) = 0.0000000000001
abs(A-B) <= 10^(-precision) = true

precision = 14
10^(-precision) = 0.00000000000001
abs(A-B) <= 10^(-precision) = false

For a project I am working on, where there are done multiple operations with real numbers, I wanted to be sure, where there are some traps I may fall in. Is it safe to compare the real numbers without custom "ApproximatelyEqual"
method?

Thanks,

Pavils

Nov 16 '05 #3
> Why you don't use double?

Using double type will not save from this problem.

Pavils
Nov 16 '05 #4
Once I had the same problem, I have to calculate some value and compare
them,
with float I have always precision errors, with double never.
"Pavils Jurjans" <pa****@mailbox.riga.lv> schrieb im Newsbeitrag
news:OK**************@tk2msftngp13.phx.gbl...
Why you don't use double?


Using double type will not save from this problem.

Pavils

Nov 16 '05 #5
You can not count on double, as it has the same problem that a float
has.

You should use the Decimal type. It will store numbers with a large
amount of decimal places accurately.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Zürcher See" <aq****@cannabismail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Once I had the same problem, I have to calculate some value and compare
them,
with float I have always precision errors, with double never.
"Pavils Jurjans" <pa****@mailbox.riga.lv> schrieb im Newsbeitrag
news:OK**************@tk2msftngp13.phx.gbl...
> Why you don't use double?


Using double type will not save from this problem.

Pavils


Nov 16 '05 #6
no, but the decimal type will, as Nicholas points out.

Any reason this won't work? I don't know what calculations you are using...
decimal isn't appropriate for every binary operation, but it is very good in
this situation because it doesn't store the number in a "true" binary
format... it stores the decimal digits themselves.

--- Nick Malik

"Pavils Jurjans" <pa****@mailbox.riga.lv> wrote in message
news:OK**************@tk2msftngp13.phx.gbl...
Why you don't use double?


Using double type will not save from this problem.

Pavils

Nov 16 '05 #7
Zürcher See <aq****@cannabismail.com> wrote:
Once I had the same problem, I have to calculate some value and compare
them, with float I have always precision errors, with double never.


You've been lucky then.

Double has more precision than float, but it's still unable to
represent variable numbers (eg 0.1) exactly.

--
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
Nick Malik <ni*******@hotmail.nospam.com> wrote:
no, but the decimal type will, as Nicholas points out.
Well, that depends.
Any reason this won't work? I don't know what calculations you are
using... decimal isn't appropriate for every binary operation, but it
is very good in this situation because it doesn't store the number in
a "true" binary format... it stores the decimal digits themselves.


Indeed. It is able to exactly represent every decimal number. Of
course, that doesn't help if you're dealing with numbers which can't be
exactly represented in decimal - such as 1/3.

If you're dealing with numbers which can always be exactly represented
in decimal even after all the operations you're interested in,
decimal's great - but it's just another floating point type with all
the associated problems really.

--
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nick Malik <ni*******@hotmail.nospam.com> wrote:
no, but the decimal type will, as Nicholas points out.
Well, that depends.


True. My "snappy reply" was overly broad and I could have qualified it
better. Unfortunately, the OP hasn't provided enough detail about his or
her calculations to make it clear if this particular numeric type would be
any more appropriate, or why they need to compare against specific real
numbered values.

We are left to guess.
Any reason this won't work? I don't know what calculations you are
using... decimal isn't appropriate for every binary operation, but it
is very good in this situation because it doesn't store the number in
a "true" binary format... it stores the decimal digits themselves.


Indeed. It is able to exactly represent every decimal number. Of
course, that doesn't help if you're dealing with numbers which can't be
exactly represented in decimal - such as 1/3.

If you're dealing with numbers which can always be exactly represented
in decimal even after all the operations you're interested in,
decimal's great - but it's just another floating point type with all
the associated problems really.


No argument here. That's why I posed the follow-up question. There are
specific mitigations for working around issues where the rational number can
be managed as a numerator and denominator until the last possible moment,
which can, depending on the calculation, maintain a bit more of the
precision. That isn't common but it does sometimes work. Other mitigations
rest with use of factors, matrix operations, formulaic representations, etc.
Once again, their applicability depends on information that the OP has not
provided.

When, or if, the OP decides to weigh in with more information, I'll be happy
to engage in a (hopefully) fruitful discussion of the uses of binary math to
solve the problem. Until then, we are, as they say in my home town of
Knoxville, "spittin' in the wind."

--- Nick
Nov 16 '05 #10
Nick Malik <ni*******@hotmail.nospam.com> wrote:
Nick Malik <ni*******@hotmail.nospam.com> wrote:
no, but the decimal type will, as Nicholas points out.
Well, that depends.


True. My "snappy reply" was overly broad and I could have qualified it
better. Unfortunately, the OP hasn't provided enough detail about his or
her calculations to make it clear if this particular numeric type would be
any more appropriate, or why they need to compare against specific real
numbered values.

We are left to guess.


Indeed :(

I guess my reason for replying was that there is a misconception around
that somehow decimals are "precise" whereas float/double aren't. Of
course, pinning down exactly what people mean by "precise" is a tricky
business - decimal/float/double are all precise, in that they exactly
represent numbers. They just don't exactly represent *all* numbers,
including the true results of many operations working with the precise
original numbers...
Any reason this won't work? I don't know what calculations you are
using... decimal isn't appropriate for every binary operation, but it
is very good in this situation because it doesn't store the number in
a "true" binary format... it stores the decimal digits themselves.


Indeed. It is able to exactly represent every decimal number. Of
course, that doesn't help if you're dealing with numbers which can't be
exactly represented in decimal - such as 1/3.

If you're dealing with numbers which can always be exactly represented
in decimal even after all the operations you're interested in,
decimal's great - but it's just another floating point type with all
the associated problems really.


No argument here. That's why I posed the follow-up question. There are
specific mitigations for working around issues where the rational number can
be managed as a numerator and denominator until the last possible moment,
which can, depending on the calculation, maintain a bit more of the
precision. That isn't common but it does sometimes work.


Yup. I seem to remember that it used to be a big performance win too,
in the right circumstances - these days FP units are probably fast
enough to counter a lot of the performance benefits of sticking with
integers.
Other mitigations
rest with use of factors, matrix operations, formulaic representations, etc.
Once again, their applicability depends on information that the OP has not
provided.

When, or if, the OP decides to weigh in with more information, I'll be happy
to engage in a (hopefully) fruitful discussion of the uses of binary math to
solve the problem. Until then, we are, as they say in my home town of
Knoxville, "spittin' in the wind."


:)

--
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

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

Similar topics

4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
2
by: mo | last post by:
Hi We've got some numbers stored as Reals which are returning values in scientific notation that we need rounded down to 3 digits to the right of the decimal. ie 8.7499999E-2 needs to return...
2
by: SAN CAZIANO | last post by:
how can i insert only a real number (an integer with only one decimal separator) in a edit field perhaps in onchange event I have to test if the decimal separator in the text is >0 I simply...
1
by: John Marble | last post by:
I looked around for a way to change the format and the decimal setting of a REAL type variable, but I can't seem to be able to find the synthax anywhere. Here is an exemple of what I am doing: ...
4
by: Homer Simpson | last post by:
I'm using VS2005 Beta 2 (C#) and would like to know how I can remove the decimal portion (to the right of the decimal) of a real number. I want the whole number portion but do not wish to discard...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
4
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a...
1
by: machina | last post by:
Hi, I would like to convert real data type to datetime type. Example: I have a real data type which is: 23,613456 (23 hours and 0,613456). I would like to have it in hh:mm:ss format. How to do...
5
by: wshaer | last post by:
Hi This is the task: and these are my classes: public class Engine{ // Declare the varibles
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.