Chapter 10

Floating-point numbers

<*********suggest changing the title

**********************The key is on page 116

*************p240 Figure 10.1 Data formats is good.

Could we use the individual small pictures with each subsection?

We also need to refer people to go look at Figure 10.1 more often.

The pictures on pages 236 thru 239 need redoing, to be consistent, and

they need to show that the zero bit is the rightmost bit.

********************************> [ 236 ]

Unsigned integers

Three new numeric data types give you closer-to-the-machine capability, closer to working in assembly language, yet you don't have to change mental gears quite as much as working directly with ASM mnemonics, and telling PowerBASIC which registers to use.

Especially in dealing with on-off flags, you can conserve memory by working at the bit level. While modern personal computers are generously endowed with cheap RAM memory, in comparison to earlier computers, there are still reasons to conserve use of memory. The most obvious one is to make your programs smaller, so that they load faster, and take up less storage space.

Once again, PowerBASIC gives you more than one way to do the same thing. It makes itself accommodating to the habits that you have, and lets you do it your way.

BYTE, WORD, and DOUBLE WORD unsigned integers let you control the use of RAM memory for variables, right down to the one, two, or four byte level.

BYTEs are like a one-character string.

WORDs are like INTEGERs, except that INTEGERs can be negative.

DOUBLE WORDs are like LONG INTEGERs, except that LONG INTEGERs can be negative.

If you want to maintain a group of flags in a small amount of space, you need to work at the bit level, and you need to be sure that the bit flags will stay the way you set them. You need certainty.

What you gain by using BYTEs, WORDs, and DOUBLE WORDs is certainty, so that you can depend upon what they say.

If you tried to use signed integers for storing bit flags, an unexpected kind of data could make your number go negative, and then all of your bit flags would be unreliable. Your whole system could crash.

What is a computer "bite"?

To the uninitiated, a "bit" is a one or zero, an "on" or an "off" in the computer, whether in the data, or in the hardware, and a "byte" is eight bits, eight ones and zeros, In the computer, it is the most basic unit of measure for where the "rubber meets the road", in data storage or handling. [ Of course, it is magnetic media, or electronic components, not real rubber. ]

PowerBASIC lets you fill a byte variable in your program with either decimal (base 10), or hexidecimal (base 16) numbers.

Hexidecimal is a higher and more convenient form of binary (base 2), that will make it easier to think in terms of actual character codes or machine language, down to the bit level.

To figure out the hexidecimal value of a number, add the value of each bit's position, according to the following, where the rightmost bit is a one, and each bit to the left is times two.

_ _ _ _ | _ _ _ _ | _ _ _ _ | _ _ _ _

3 1 8 4 2 1 5 2 1 6 3 1 8 2 4 1

2 6 1 0 0 0 1 5 2 4 2 6

7 3 9 9 4 2 2 6 8

6 8 2 6 8 4

8 4

*****************be sure that these are aligned

Byte (?)

Can be compared to chr$(), or a one-character string

Length 1 byte or 8 bits Range 0 to 255

Negative values cannot be stored in an unsigned integer.

PowerBASIC provides three different ways to define a variable as a byte unsigned integer:

Following the variable name with one question mark (?)

In a DEFtype statement DEFBYT variable name

In a dimension statement DIM I AS BYTE

Convenient for storage of small numbers, or a set of 8 bit flags.

Example

rem Instead of using

x%=csrlin: y%=pos(0): ' to save and restore cursor position

rem try using

x?=csrlin: y?=pos(0)

See Figure 10.1, Data formats, on page 240. See also page 120.

Word (??)

Can be compared to Integers(%)

Length 2 bytes or 16 bits Range 0 to 65535

Negative values cannot be stored in an unsigned integer.

The least significant byte is stored first in memory, then the most-significant byte in the next memory location, in what humans would consider reverse order.

PowerBASIC provides three different ways to define a variable as a word unsigned integer:

Following the variable name with two question marks (??)

In a DEFtype statement DEFWRD variable name

In a dimension statement DIM I AS WORD

Use when an INTEGER is not large enough for your variable. A signed integer can only go as high as 32,767, because the sign bit is not available. A WORD variable can go as high as 65535.

Useful to indicate the offset address for PEEK and POKE.

Convenient for storage of a set of 16 bit flags.

Example

rem Instead of using

x%=VARPTR(XX$)

rem try using

x??=VARPTR(XX$)

See Figure 10.1, Data formats, on page 240. See also page 120.

Double Word (???)

Can be compared to Long Integers(&)

Length 4 bytes or 32 bits Range 0 to 4,294,967,295

Negative values cannot be stored in an unsigned integer.

The least significant byte of each word is stored first in memory, then the most-significant byte in the next memory location, in what humans would consider reverse order. The least significant word is stored first in memory, then the most-significant word.

PowerBASIC provides three different ways to define a variable as a double word unsigned integer:

Following the variable name with three question marks (???)

In a DEFtype statement DEFDWD variable name

In a dimension statement DIM I AS DWORD

Use when a LONG integer is not large enough for your variable.

Use to indicate absolute memory addresses for PEEK and POKE.

Convenient for storage of a set of 32 bit flags.

See examples in the PowerBASIC Reference Guide, page 249, under POKE.

See Figure 10.1, Data formats, on page 240. See also page 121.

********************************> [ 243 ]

Binary coded decimal

Binary coded decimal (BCD) is the most accurate way of handling and manipulating floating-point numbers. This technique is the slowest to run. When dollars really matter, use BCD. It is more trustworthy for financial or accounting programs, because round-off errors are eliminated.

See page 125 for more detail on binary coded decimal numbers.

BCD fixed point (@)

PowerBASIC stores BCD fixed-point values in a proprietary format, that uses 8 bytes of memory, and has a precision of 18 decimal points. The range is the same as extended precision, of approximately - 9.99 x 10 -63 - 9.99 x 10+63

*********>>>>> fix this with superscripts and a plus-minus in front !!!!

A BCD fixed-point number is an 8-byte signed quad integer. A system

variable named FIXDIGITS keeps count of how many digits (from 0 to 18)

are on the right of the decimal point.

FIXDIGITS

true value = integer / 10

Example

Test@=1.1: ' value will be 1.100000023841858

Test@=1.1@: ' value will be 1.1

See Figure 10.1, Data formats, on page 240.

BCD floating point (@@)

PowerBASIC stores BCD floating-point values in a proprietary format, that uses 10 bytes of memory, and has a precision of 18 decimal points. The range is the same as extended precision, of approximately - 9.99 x 10 -63 - 9.99 x 10+63

*********this with superscripts and a plus-minus in front !!!!

8-byte signed quad integer, and a signed 2-byte exponent, with a bias of

17 (&H11).

true value = integer / 10-17

Example

You can use the @@ identifier after the variable name, and after the

number used to fill it.

Test@@=1.1: ' value will be 1.100000023841858

Test@@=1.1@@: ' value will be 1.1

See Figure 10.1, Data formats, on page 240.

This file has been created with the evaluation or unregistered copy of
EasyHelp from Eon Solutions
(Tel: UK 0973 209667, Email: eon@cix.compulink.co.uk)