Floating-point datatypes in ASE
Contents:
Some platforms use different standards. On the VAX, for instance, a double uses a 56-bit mantissa, an 8-bit exponent, and one sign bit for a total of 64 bits.
Mantissa and exponent
Example
^ ^ ^ ^
| | | exponent
| | |
| | mantissa
| |
| virtual bit of mantissa (always on)
|
sign bit
- determine "is the number positive or negative?". If it is negative, set the sign bit.
- Next determine "what is the smallest power of 2 that is larger than the number?".
- Subtract one from that power to find the exponent of the implicit bit in the mantissa. Store that exponent in the exponent field.
- Subtract the value of the implicit bit (2^exponent) from the number.
- Compare 2^(exponent-1) to the remainder.
- If 2^(exponent-1) is less than the remainder, set the next bit and subtract the value of that bit from the number.
- Compare 2^(exponent-2) to the remainder.
- Repeat in this manner until you run out of mantissa bits.
The number is positive, so the sign bit is set to 0:
0 [1]11######### 0110
27.3 - 16 = 11.3. 2^3 is 8, which is smaller than 11.3, so the next bit is set:
0 [1]111######## 0110
11.3 - 8 = 3.3. 2^2 is 4, which is larger than 3.3, so the next bit is not set:
0 [1]1110####### 0110
3.3 - 0 = 3.3. 2^1 is 2, which is smaller than 3.3, so the next bit is set:
0 [1]11101###### 0110
3.3 - 2 = 1.3. 2^0 is 1, which is smaller than 1.3, so the next bit is set:
0 [1]111011##### 0110
1.3 - 1 = 0.3. 2^-1 is 0.5, which is larger than 0.3, so the next bit is not set:
0 [1]1110110#### 0110
0.3 - 0 = 0.3. 2^-2 is 0.25, which is smaller than 0.3, so the next bit is set:
0 [1]11101101### 0110
0.3 - 0.25 = 0.05. 2^-3 is 0.125, which is larger than 0.05, so the next bit is not set:
0 [1]111011010## 0110
0.05 - 0 = 0.05. 2^-4 is 0.06125, which is larger then 0.05, so the next bit is not set:
0 [1]1110110100# 0110
0.05 - 0 = 0.05. 2^-5 is 0.030625, which is smaller than 0.05, so the next bit is set:
0 [1]11101101001 0110
This represents the actual value:
64 + 32 + 16 + 8 + 2 + 1 + 0.25 + 0.030625 = 123.280625
123.3 - 123.280625 is an error of 0.019375.
It may be possible to reduce the error by rounding up to the next larger number that can be represented (ie, add 2^-5). This works out to:
0 [1]11101101001 0110
This is a smaller error, so the representation is rounded to:
Frequently Asked Questions
1) Why doesn't round() work right with floating point? There is garbage in the sixth decimal place when I round a floating point number, as in:
The decimal rounded value of 123.31 is 123.3, but the real datatype cannot store 123.3 exactly. The garbage is due to the imprecision inherent in the floating point format. There is also a display issue: by default, isql displays floats with 6 digits after the decimal point. Some front-end programs are more intelligent about this: they know how many digits of the number will be accurate and truncate or round the display at that point. You can use the T-SQL str() function for better control over the display of floating point data (str() is documented under "String Functions" in the ASE manuals.) For example, here we rounded to one decimal place, so there is no need to display past one decimal place:
2) So just how inaccurate are reals and doubles?
3) When I declare a column or variable to be of type float, there is an optional [precision] specification. What effect does this have?
Other than this, the [precision] specification has no effect. The syntax may seem somewhat pointless, but it allows for compatibility with DDL developed for other systems that interpret [precision] differently.
4) So floating point only has problems storing fractions, right?
The smallest value for which we see this is 2^24+1. reals can store upto 2^24 with no problem (the implicit bit is on, the exponent is set to 24, and all the other mantissa bits are zeroed); but 2^24+1 requires 22 zero bits and a final bit following the implicit bit (24 bits total, only 23 available).
1> select power(2,24)-1, power(2,24), power(2,24)+1
5) I don't see this behavior in my C/C++ program. What's up?
=========================================
main()
/* Program to demonstrate floating point imprecision */
{
float r;
double d ;
r = 123.3;
d = 123.3;
printf("As a %d-byte float (real): 123.3 is %48.24f
\n",
sizeof(r),r);
printf("As a %d-byte double: 123.3 is %48.24f \n", sizeof(d),d);
}
Sample output on Solaris 2.5:
alliance1{bret}125: a.out
As a 4-byte real: 123.3 is
123.300003051757812500000000
As a 8-byte double: 123.3 is
123.299999999999997157829057
=========================================
For C++:
=========================================
#include <iostream.h>
#include <iomanip.h>
main()
-- Program to demonstrate floating point inaccuracy.
{
int precision;
float y;
y = 123.3;
cout << "123.3
as a float printed with increasing precision" <<
endl;
cout <<
"-------------------------------------------------------------"
<< endl;
for (precision = 1;
precision < 30; precision++)
{
cout.precision(precision);
cout <<precision
<< " " << y << endl;
}
double x;
x = 123.3;
cout << endl;
cout << "123.3
as a double, printed with increasing precision"
<< endl;
cout <<
"-------------------------------------------------------------"
<< endl;
for (precision = 1; precision
< 30; precision++)
{
cout.precision(precision);
cout <<precision
<< " " << x << endl;
}
}
Sample output on Solaris 2.5:
alliance1{bret}140: a.out
123.3 as a float printed with increasing precision
-------------------------------------------------------------
1 1e+02
2 1.2e+02
3 123
4 123.3
5 123.3
6 123.3
7 123.3
8 123.3
9 123.300003
10 123.3000031
11 123.30000305
12 123.300003052
13 123.3000030518
14 123.30000305176
15 123.300003051758
16 123.3000030517578
17 123.30000305175781
18 123.300003051757812
19 123.3000030517578125
20 123.3000030517578125
21 123.3000030517578125
22 123.3000030517578125
23 123.3000030517578125
24 123.3000030517578125
25 123.3000030517578125
26 123.3000030517578125
27 123.3000030517578125
28 123.3000030517578125
29 123.3000030517578125
123.3 as a double, printed with increasing precision
-------------------------------------------------------------
1 1e+02
2 1.2e+02
3 123
4 123.3
5 123.3
6 123.3
7 123.3
8 123.3
9 123.3
10 123.3
11 123.3
12 123.3
13 123.3
14 123.3
15 123.3
16 123.3
17 123.3
18 123.299999999999997
19 123.2999999999999972
20 123.29999999999999716
21 123.299999999999997158
22 123.2999999999999971578
23 123.29999999999999715783
24 123.299999999999997157829
25 123.2999999999999971578291
26 123.29999999999999715782906
27 123.299999999999997157829057
28 123.299999999999997157829057
29 123.29999999999999715782905696
6) Where can I find more information on floating points?
Many books on assembly language programming go into great detail.A search on the World Wide Web for keywords "IEEE" and "floating" will provide many documents of interest.

Back to Top