473,657 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.2345670000000 000000003454786 544 or
1.2345669999999 999999999999999 99999924354324.

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 "ApproximatelyE qual" method, for
comparing two real numbers and providing precision:

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

Example:
A = 1.0000000000000 1
B = 1.0000000000000 7
precision = 13
abs(A-B) = 0.0000000000000 6
10^(-precision) = 0.0000000000001
abs(A-B) <= 10^(-precision) = true

precision = 14
10^(-precision) = 0.0000000000000 1
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 "ApproximatelyE qual"
method?

Thanks,

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

"Pavils Jurjans" <pa****@mailbox .riga.lv> schrieb im Newsbeitrag
news:e8******** ******@TK2MSFTN GP10.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.2345670000000 000000003454786 544 or
1.2345669999999 999999999999999 99999924354324.

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 "ApproximatelyE qual" method, for comparing two real numbers and providing precision:

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

Example:
A = 1.0000000000000 1
B = 1.0000000000000 7
precision = 13
abs(A-B) = 0.0000000000000 6
10^(-precision) = 0.0000000000001
abs(A-B) <= 10^(-precision) = true

precision = 14
10^(-precision) = 0.0000000000000 1
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 "ApproximatelyE qual"
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******** ******@TK2MSFTN GP10.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.2345670000000 000000003454786 544 or
1.2345669999999 999999999999999 99999924354324.

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 "ApproximatelyE qual" method, for comparing two real numbers and providing precision:

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

Example:
A = 1.0000000000000 1
B = 1.0000000000000 7
precision = 13
abs(A-B) = 0.0000000000000 6
10^(-precision) = 0.0000000000001
abs(A-B) <= 10^(-precision) = true

precision = 14
10^(-precision) = 0.0000000000000 1
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 "ApproximatelyE qual"
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******** ******@tk2msftn gp13.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.co m
"Zürcher See" <aq****@cannabi smail.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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****@cannabi smail.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Nick Malik <ni*******@hotm ail.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Nick Malik <ni*******@hotm ail.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

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

Similar topics

4
2041
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. Bigint math isn't much slower, for integers that all fit within an integer. 2) Converting float to varchar is relatively slow, and should be avoided if possible. Converting from integer to varchar or varchar to int is several times faster.
2
5196
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 8.75. Round, cast, convert, formatnumber in the dts package all fail. Help! Thanks Moe
2
1991
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 have to put it as a null key
1
2532
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: ALTER TABLE MAIN ALTER COLUMN sup_visee REAL; It works, it changes the column "sup_visee" which was a String into a REAL. What I want to do too is to be able to set the REAL format to
4
17120
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 the decimal portion; that will be used for additional calcs. Does C# have any methods for doing this? Thanks, Scott
17
4361
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 problem writing VB Double values to the file so as the Pascal program can read them as Pascal Real values. I've managed to find the algorithm to read the Pascal Real format and convert it to a VB Double, but I cannot figure out the opposite...
4
3368
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 separate answer for each version of the language. Let's consider the conversion from a given floating real type to a specific integer type.
1
4058
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 this? Can I use convert/cas function? Thanks for help Rgds Mario
5
3710
by: wshaer | last post by:
Hi This is the task: and these are my classes: public class Engine{ // Declare the varibles
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.