home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Press 1997 July
/
Sezamfile97_1.iso
/
msdos
/
database
/
pdx_ti2.arj
/
TI1148.ASC
< prev
next >
Wrap
Text File
|
1992-11-12
|
11KB
|
463 lines
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 1/7
TITLE : How computers and Paradox use numbers
Intended Audience:
All Paradox users. Some sections are aimed at PAL programmers.
Purpose:
This Tech Info Sheet explains how computers handle numbers,
describes the Paradox numeric formats, and gives solutions to
some common problems.
When you write a number down, you normally use as many digits as
needed. For example, the new car you bought might have cost
$16,758.35. If you were talking to friends, though, you would
probably just say, "That car cost me sixteen-point-seven thou."
Look at the United States budget, and you would not even try to
figure it down to the nearest penny, eyeballing it as pretty
close to two trillion dollars.
Let us suppose that you are only allowed to use 8 digits to
represent any number. Pretend for a moment that the actual U.S.
budget is $2,173,516,948,243.82. That is a total of 15 digits.
In order to make our 15 digit number fit into 8 slots, we are
going to need to do two things: round the number, and convert it
to scientific notation.
Thus, we end up with 0.217352 x 10^13. If you strip out the
extra characters, you can write it down in 8 digits as 21735213,
where the first six digits are the mantissa, and the last two
digits are the exponent.
This is very similar to the way computers operate: in order to
speed up computations, they allocate a fixed space to store
numbers. The problem with this approach is that you can lose
some information about the number, due to the rounding. Some
computers do store numbers the way humans do, but they are a lot
slower; this technique is called Binary Coded Decimal (BCD).
In addition to the rounding issue, computers also have to convert
between human numbers, which is typically decimal notation, and
computer numbers, which use binary notation. For example, the
following two numbers are equivalent:
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 2/7
TITLE : How computers and Paradox use numbers
180 10110100
Decimal Binary
But, there are problems when you start dealing with fractions.
Consider the following:
Take the fraction 1/3. Now convert it to a decimal. You should
end up with 0.33333333.... repeating. If the number is truncated
at three positions, you get 0.333. If you do the same process to
2/3, you start with 0.66666666..., then go to 0.666 -- or should
it be 0.667?
Let us assume that 2/3 is converted to 0.666. Then, 1/3 + 2/3
equals 0.999!
In actual practice, computers usually use enough decimal places
that you will rarely see a difference in the calculation. The
only real problem lies when you subtract the results of two long
calculations to see if the result is zero.
Paradox uses two standard numeric formats, fixed position integer
and floating point numbers. Integers take up two characters, and
have a range of ±32,767. Because there is no fractional portion,
integers have no rounding problem, but the range of numbers may
not be sufficient. Integers are equivalent to the S type in
Paradox.
Floating point numbers use 8 bytes, and can represent numbers
with a range of 10 x ±308, and have approximately 15 digits of
precision. You can use either N or $ for floating point, the
only difference is the formatting.
NOTE: For financial applications that need to be accurate to the
nearest penny, the maximum amount that can be used is $100
billion.
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 3/7
TITLE : How computers and Paradox use numbers
SOLUTIONS TO COMMON PROBLEMS:
NOTE: If there is any data from Paradox 3.0 or earlier, you must
do the following steps, as prescribed in the Paradox 3.5 README
file. The binary conversion routines were changed in Paradox
3.5, so that numbers round differently. For example, if you
entered $152.85 in 3.0, then tried to Zoom for it in 3.5, Paradox
would not find it. This has only a slight effect on how numbers
get handled, so calculations should produce almost the same
result.
If you have two numbers that should be equal (i.e. subtract to
zero), but Paradox does not bring them up when you link them, the
simplest solution is to do the following (the example assumes
that the table is called Test):
Tools | ExportImport | Export | Ascii | Delimited |
Test | Test
Tools | More | Empty | Test | Ok
Tools | ExportImport | Import | Ascii | AppendDelmited |
Test | Test
This exports the data to ASCII, empties the table, then reimports
the data. This technique will not work if the decimal numbers
are different; for that, you need to do rounding.
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 4/7
TITLE : How computers and Paradox use numbers
NOTE: All methods from this point forward assume a general
knowledge of PAL programming.
Often times, a calculation involving multiplication or division
will result in a value that has more digits than needed -- or
wanted. This is particularly true in financial calculations,
where the numbers need to be rounded to the nearest penny every
step of the way.
One way to handle this is store the numbers as pennies: instead
of $537.25, you store 53725. You can then use the INT() or
ROUND() functions to handle any excess digits. The primary
advantage of this method is that there is no rounding error
during addition and subtraction.
The difficulty comes from handling data entry and display of the
data. Typically, a PAL program allows the user to enter numbers
conventionally (e.g. 93.67), then multiplies by 100 (to 9367).
As a general rule for financial calculations, the ROUND()
function should be used after every multiplication and division.
For most other calculations, ROUND() or INT() should be used only
on the final result.
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 5/7
TITLE : How computers and Paradox use numbers
ROUND()
The ROUND() function in Paradox 4.0 has been updated to use
"Banker's Rounding" in its algorithm. Most users should be
unaffected by this change, described below:
In standard rounding, whenever a number lies exactly on the
halfway point (e.g. 223.65 rounded to one place), you round up
(getting 223.7). In Banker's Rounding, you round to the nearest
even number (in this case, 223.6).
Here is an example that explains why. Suppose you have the
following string of numbers, with their equivalents rounded the
standard way and with Banker's Rounding; underneath is the total:
Original Standard Banker's
15.845 15.85 15.84
21.395 21.40 21.40
3.165 3.17 3.16
13.355 13.36 13.36
------ ----- -----
53.760 53.78 53.76
By adding the numbers up with the standard rounding technique,
the final answer is two pennies high.
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 6/7
TITLE : How computers and Paradox use numbers
If Banker's Rounding is not desired, the following procedure will
work:
PROC NormalRound (N, Digits)
PRIVATE Shift
Shift = POW (10, Digits)
RETURN INT ( ( N * Shift ) + IIF(N<0, -.5, .5) ) / Shift
ENDPROC
For Paradox 3.5 and earlier, use the following instead:
PROC NormalRound (N, Digits)
PRIVATE Shift, RoundFactor
Shift = POW (10, Digits)
IF N < 0 THEN
RoundFactor = -.5
ELSE
RoundFactor = .5
ENDIF
RETURN INT ( ( N * Shift ) + RoundFactor ) / Shift
ENDPROC
To understand how NormalRound() works, here is a walkthrough on
what happens when you round to two decimal places:
N = 542.6475 ; Starting number
N = 54264.75 ; Multiply by 100, shift 2 places
N = 54265.25 ; Add 0.5
N = 54265. ; Take the INT()
N = 542.65 ; Divide by 100, shift it back
PRODUCT : Paradox NUMBER : 1148
VERSION : All
OS : DOS
DATE : November 12, 1992 PAGE : 7/7
TITLE : How computers and Paradox use numbers
PARADOX 3.5
For version 3.5 only, the ROUND() and INT() functions can return
incorrect values with negative numbers. Internally, Paradox is
storing the number as -0, an impossible number. The solution is
to add 0 to the expression:
IF 0 + ROUND(Total,2) = 0 THEN ......
MISCELLANEOUS TECHNIQUES
In some rare circumstances, a situation similar to the conversion
problem between 3.0 and 3.5/4.0 can occur. Supposing that the
value was in the field [Total], the following PAL code should
correct the problem:
[Total] = NUMVAL(STRVAL([Total]))
When a PAL variable is assigned from a $ (currency) field, it --
and all values derived from it -- gets internally formatted
identically to a currency field. This can make it difficult to
determine what the actual value in the variable is, because it
gets rounded on the screen. Assuming that the variable is called
Total, the following code changes Total to a normal numeric
format:
Total = NUMVAL ( FORMAT ("W25.17,ES", Total) )
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.