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

What Algorithm Do Single and Double Use when Storing Imprecise Values?

I know that Single and Double can only store values that can be stored as
x/2^y, such as 0.5 and 0.125. But when they attempt to store values such as
0.1 or 0.2 whose exact value cannot be stored, what algorithm do they use to
determine what value to store? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Oct 19 '08 #1
6 1427
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
>I know that Single and Double can only store values that can be stored as
x/2^y, such as 0.5 and 0.125. But when they attempt to store values such as
0.1 or 0.2 whose exact value cannot be stored, what algorithm do they use
to determine what value to store? Thanks.
They use a mantissa and an exponent, so that a number is expressed as
(mantissa)*10^(exponent). We do this frequently in everyday engineering work
when we say for instance, that something took "1.23*10^-6 seconds". However,
when speaking about Single and Double, the "10" has to be interpreted in
base 2 (meaning that it is a "2" in base 10). The exponent and mantissa are
also in base 2. The precission of the mantissa is limited, because the
mantissa and exponent and their signs have to fit into 4 bytes (Single) or 8
bytes (Double). The numbers that can be represented exactly are those that
can be converted into base 2 within that number of bits. For instance, 0.5
(base10) is 0.1 (base 2), but some numbers that have a small number of
decimals in base 10 have an infinite number of decimal places once converted
to base 2, so they will be truncated when assigned to the mantissa, and
therefore they will not be "exact".

Oct 19 '08 #2
"Nathan Sokalski" <nj********@hotmail.comschrieb
>I know that Single and Double can only store values that can be stored as
x/2^y, such as 0.5 and 0.125. But when they attempt to store values such as
0.1 or 0.2 whose exact value cannot be stored, what algorithm do they use
to determine what value to store? Thanks.
As the documentation
http://msdn.microsoft.com/en-us/libr...em.single.aspx
says, the types comply with IEEE 754 which also defines rounding rules.
Search for it; you'll find wikipedia etc.

Armin

Oct 19 '08 #3
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
>I know that Single and Double can only store values that can be stored as
x/2^y, such as 0.5 and 0.125. But when they attempt to store values such as
0.1 or 0.2 whose exact value cannot be stored, what algorithm do they use
to determine what value to store? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Here's a PDF version
http://www.physics.ohio-state.edu/~d...point_math.pdf
Oct 19 '08 #4
Ken Halter wrote:
What Every Computer Scientist Should Know About Floating-Point
Arithmetic http://docs.sun.com/source/806-3568/ncg_goldberg.html

Here's a PDF version
http://www.physics.ohio-state.edu/~d...point_math.pdf

Interesting link, thanks. I can use such a .pdf from time to time. It
is amazing that CS people know so little about it.

--
Rudy Velthuis http://rvelthuis.de

"I once heard two ladies going on and on about the pains of
childbirth and how men don't seem to know what real pain is. I
asked if either of them ever got themselves caught in a zipper."
-- Emo Philips.
Oct 19 '08 #5
I understand how they are stored, that is very simple. My question was what
algorithm they use to determine what value to store. Let me rephrase what I
meant by this. When a value is assigned to a variable that cannot be stored
as an exact value, such as the following:

Dim x As Byte = 1.2

Since the exact value cannot be stored, what algorithm is used to determine
what value will be stored? Since the value is obviously not exact, it must
be rounded up or down, so how do I know which it is?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP02.phx.gbl...
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
>>I know that Single and Double can only store values that can be stored as
x/2^y, such as 0.5 and 0.125. But when they attempt to store values such
as 0.1 or 0.2 whose exact value cannot be stored, what algorithm do they
use to determine what value to store? Thanks.

They use a mantissa and an exponent, so that a number is expressed as
(mantissa)*10^(exponent). We do this frequently in everyday engineering
work when we say for instance, that something took "1.23*10^-6 seconds".
However, when speaking about Single and Double, the "10" has to be
interpreted in base 2 (meaning that it is a "2" in base 10). The exponent
and mantissa are also in base 2. The precission of the mantissa is
limited, because the mantissa and exponent and their signs have to fit
into 4 bytes (Single) or 8 bytes (Double). The numbers that can be
represented exactly are those that can be converted into base 2 within
that number of bits. For instance, 0.5 (base10) is 0.1 (base 2), but some
numbers that have a small number of decimals in base 10 have an infinite
number of decimal places once converted to base 2, so they will be
truncated when assigned to the mantissa, and therefore they will not be
"exact".

Oct 20 '08 #6
Since the exact value cannot be stored, what algorithm is used to
determine what value will be stored? Since the value is obviously not
exact, it must be rounded up or down, so how do I know which it is?
But you've already been told: IEEE 754.

SteveT

Oct 20 '08 #7

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

Similar topics

112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
5
by: towers | last post by:
Hello, I've got a bit of experience in C++, but I'm writing my first app that is dependent on relatively precise math functions. The app requires that I get a time stamp based on s sample...
2
by: sparks | last post by:
ok I was trying to do something real simple I thought Me.Clinic.DefaultValue = Me.Clinic.Value I got error either ! or . misused or not found...something like that I thought ok how can you not...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
4
by: IceMan | last post by:
Hello I am getting some odd answers when i use doubles in c# 0.55-0.5 = 0.050000000000000044 why am i not getting 0.05
6
by: Nathan Sokalski | last post by:
I know that Single and Double can only store values that can be stored as x/2^y, such as 0.5 and 0.125. But when they attempt to store values such as 0.1 or 0.2 whose exact value cannot be stored,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.