MATHEMATIC ROUTINES: LIMITS OF NUMERIC TYPES

This example attempts to show the behavior of the system when dealing with conversion between types, signed or unsigned. The rule that ugBASIC uses is the minimum error rule: in general, if the destination type has a sufficient number of bits, it is possible to represent the information without losses. If the destination type has fewer bits, the number is guaranteed to be represented correctly if the number of destination bits are sufficient. In case of signed types, if the source number is negative but the destination type is unsigned, the value will be set to the absolute value without sign.

source compile sandbox issues? back to examples

SOURCE CODE ()

PROCEDURE example ON ALL BUT VIC20, PLUS4

    CLS

    ' ******************************************************************
    ' ******** destination: signed byte ********************************
    ' ******************************************************************

    DIM sb AS SIGNED BYTE

    ' source underflow -> destination underflow
    '
    ' -42 = 11010110 -> 11010110 -> -42
    sb = -42
    PRINT "signed byte(-42) = "; sb; " (exp ";"-42";" )"

    ' source overflow [with unsigned byte] -> destination approximation
    '
    ' 200 = 11001000 -> 01001000 -> +72
    sb = 200
    PRINT "signed byte(200) = "; sb; " (exp ";"72";" )"

    ' source overflow [with signed word] -> destination approximation
    '
    ' -500 = 1111111000001100 -> 000111110100 -> 11110100 -> -116
    sb = -500
    PRINT "signed byte(-500) = "; sb; " (exp ";"-116";" )"

    ' source overflow [with unsigned word] -> destination approximation
    '
    ' 20000 = 0100111000100000 -> 00100000 -> +32
    sb = 20000
    PRINT "signed byte(20000) = "; sb; " (exp ";"32";" )"

    ' source overflow [with signed dword] -> destination approximation
    '
    ' -100500 = 11111111111111100111011101101100 -> 00011000100010010100 -> 10010100
    sb = -100500
    PRINT "signed byte(-100500) = "; sb; " (exp ";"-20";" )"

    ' source overflow [with unsigned dword] -> destination approximation

    sb = 10020000
    PRINT "signed byte(10020000) = "; sb; " (exp ";"32";" )"

    ' ******************************************************************
    ' ******************************************************************
    ' ******************************************************************

    WAIT KEY RELEASE : PRINT

    ' ******************************************************************
    ' ******** destination: unsigned byte ******************************
    ' ******************************************************************

    DIM b AS BYTE

    ' source underflow -> destination underflow
    '
    ' 42 = 11010110 -> 11010110 -> 42
    b = 42
    PRINT "byte(42) = "; b; " (exp ";"42";" )"

    ' source overflow [with unsigned byte] -> destination unsigned
    '
    ' -42 = 11010110 -> 01001000 -> +42
    b = -42
    PRINT "byte(-42) = "; b; " (exp ";"42";" )"

    ' source overflow [with signed word] -> destination approximation
    '
    ' -500 = 1111111000001100 -> 000111110100 -> 11110100 -> -116
    b = -500
    PRINT "byte(-500) = "; b; " (exp ";"12";" )"

    ' source overflow [with unsigned word] -> destination approximation
    '
    ' 20000 = 0100111000100000 -> 00100000 -> +32
    b = 20000
    PRINT "byte(20000) = "; b; " (exp ";"32";" )"

    ' source overflow [with signed dword] -> destination approximation
    '
    ' -100500 = 11111111111111100111011101101100 -> 00011000100010010100 -> 10010100
    b = -100500
    PRINT "byte(-100500) = "; b; " (exp ";"148";" )"

    ' source overflow [with unsigned dword] -> destination approximation

    b = 10020000
    PRINT "byte(10020000) = "; b; " (exp ";"160";" )"

    ' ******************************************************************
    ' ******************************************************************
    ' ******************************************************************

    WAIT KEY RELEASE : PRINT

    ' ****************************************************************
    ' ******** destination: signed word ******************************
    ' ****************************************************************

    DIM sw AS SIGNED WORD

    ' source underflow -> destination underflow
    '
    ' 42 = 11010110 -> 11010110 -> 42
    sw = 42
    PRINT "signed word(42) = "; sw; " (exp ";"42";" )"

    ' source underflow [with unsigned byte] -> destination underflow
    '
    ' -42 = 11010110 -> -42
    sw = -42
    PRINT "signed word(-42) = "; sw; " (exp ";"-42";" )"

    ' source underfow [with signed word] -> destination underflow
    '
    ' -500 = 1111111000001100 -> -500
    sw = -500
    PRINT "signed word(-500) = "; sw; " (exp ";"-500";" )"

    ' source overflow [with unsigned word] -> destination approximation
    '
    ' 40000 = 0100111000100000 -> 00100000 -> 7232
    sw = 40000
    PRINT "signed word(40000) = "; sw; " (exp ";"7232";" )"

    ' source overflow [with signed dword] -> destination approximation
    '
    ' -100500 = 11111111111111100111011101101100 -> 00011000100010010100 -> 10010100
    sw = -100500
    PRINT "signed word(-100500) = "; sw; " (exp ";"-2196";" )"

    ' source overflow [with unsigned dword] -> destination approximation

    sw = 10020000
    PRINT "signed word(10020000) = "; sw; " (exp ";"25760";" )"

    ' ******************************************************************
    ' ******************************************************************
    ' ******************************************************************

    WAIT KEY RELEASE : PRINT

    ' ******************************************************************
    ' ******** destination: unsigned word ******************************
    ' ******************************************************************

    DIM w AS WORD

    ' source underflow -> destination underflow
    '
    ' 42 = 11010110 -> 11010110 -> 42
    w = 42
    PRINT "word(42) = "; w; " (exp ";"42";" )"

    ' source underflow [with unsigned byte] -> destination underflow
    '
    ' -42 = 11010110 -> -42
    w = -42
    PRINT "word(-42) = "; w; " (exp ";"42";" )"

    ' source underfow [with signed word] -> destination underflow
    '
    ' -500 = 1111111000001100 -> -500
    w = -500
    PRINT "word(-500) = "; w; " (exp ";"500";" )"

    ' source overflow [with unsigned word] -> destination underflow
    '
    ' 40000 = 0100111000100000 -> 00100000 -> 7232
    w = 40000
    PRINT "word(40000) = "; w; " (exp ";"40000";" )"

    ' source overflow [with signed dword] -> destination approximation
    '
    ' -100500 = 11111111111111100111011101101100 -> 00011000100010010100 -> 10010100
    w = -100500
    PRINT "word(-100500) = "; w; " (exp ";"30752";" )"

    ' source overflow [with unsigned dword] -> destination approximation

    w = 10020000
    PRINT "word(10020000) = "; w; " (exp ";"58528";" )"

    ' ******************************************************************
    ' ******************************************************************
    ' ******************************************************************

    WAIT KEY RELEASE : PRINT

    ' *****************************************************************
    ' ******** destination: signed dword ******************************
    ' *****************************************************************

    DIM dw AS SIGNED DWORD

    ' source underflow -> destination underflow
    '
    ' 42 = 11010110 -> 11010110 -> 42
    dw = 42
    PRINT "signed dword(42) = "; dw; " (exp ";"42";" )"

    ' source underflow [with unsigned byte] -> destination underflow
    '
    ' -42 = 11010110 -> -42
    dw = -42
    PRINT "signed dword(-42) = "; dw; " (exp ";"-42";" )"

    ' source underfow [with signed word] -> destination underflow
    '
    ' -500 = 1111111000001100 -> -500
    dw = -500
    PRINT "signed dword(-500) = "; dw; " (exp ";"-500";" )"

    ' source underflow [with unsigned word] -> destination underflow
    '
    ' 40000 = 0100111000100000 -> 00100000 -> 7232
    dw = 40000
    PRINT "signed dword(40000) = "; dw; " (exp ";"40000";" )"

    ' source underflow [with signed dword] -> destination underflow
    '
    ' -100500 = 11111111111111100111011101101100 -> 00011000100010010100 -> 10010100
    dw = -100500
    PRINT "signed dword(-100500) = "; dw; " (exp ";"-100500";" )"

    ' source underflow [with unsigned dword] -> destination approximation

    dw = 10020000
    PRINT "signed dword(10020000) = "; sw; " (exp ";"25760";" )"

    ' ******************************************************************
    ' ******************************************************************
    ' ******************************************************************

    WAIT KEY RELEASE : PRINT

    ' ******************************************************************
    ' ******** destination: unsigned word ******************************
    ' ******************************************************************

    DIM udw AS DWORD

    ' source underflow -> destination underflow
    '
    ' 42 = 11010110 -> 11010110 -> 42
    udw = 42
    PRINT "unsigned dword(42) = "; udw; " (exp ";"42";" )"

    ' source underflow [with unsigned byte] -> destination underflow
    '
    ' -42 = 11010110 -> -42
    udw = -42
    PRINT "unsigned dword(-42) = "; udw; " (exp ";"42";" )"

    ' source underfow [with signed word] -> destination underflow
    '
    ' -500 = 1111111000001100 -> -500
    udw = -500
    PRINT "unsigned dword(-500) = "; udw; " (exp ";"500";" )"

    ' source underflow [with unsigned word] -> destination underflow
    '
    ' 40000 = 0100111000100000 -> 00100000 -> 7232
    udw = 40000
    PRINT "unsigned dword(40000) = "; udw; " (exp ";"40000";" )"

    ' source underflow [with signed dword] -> destination underflow
    '
    ' -100500 = 11111111111111100111011101101100 -> 00011000100010010100 -> 10010100
    udw = -100500
    PRINT "unsigned dword(-100500) = "; udw; " (exp ";"-100500";" )"

    ' source underflow [with unsigned dword] -> destination underflow

    udw = 10020000
    PRINT "unsigned dword(10020000) = "; udw; " (exp ";"10020000";" )"

END PROCEDURE

    example[] ON ALL BUT VIC20, PLUS4

How to compile and run the example

The instructions here refer to compiling the example from the command line. For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile each single example with just one click.



Are instructions for your specific home computer / console missing? First of all, check if your computer is supported by clicking here. If so, since ugBASIC is a language which does not provide abstractions, it is possible that this example will not work on your target. If you think this is an issue, please click here.

Amstrad CPC 464, Amstrad CPC 6128, Amstrad CPC 664

In order to compile the example, type this command on the command line:

Linux

ugbc.cpc -O dsk -o maths_relative_03.dsk maths_relative_03.bas

Windows

ugbc.cpc.exe -O dsk -o maths_relative_03.dsk maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Atari 1200XL, Atari 130XE, Atari 600XL, Atari 65XE, Atari 800XE, Atari 800XL, Atari XEGS

In order to compile the example, type this command on the command line:

Linux

ugbc.atarixl -O xex -o maths_relative_03.xex maths_relative_03.bas

Windows

ugbc.atarixl.exe -O xex -o maths_relative_03.xex maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Atari 400, Atari 800

In order to compile the example, type this command on the command line:

Linux

ugbc.atari -O xex -o maths_relative_03.xex maths_relative_03.bas

Windows

ugbc.atari.exe -O xex -o maths_relative_03.xex maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Coleco Vision, Dina (Chuang Zao Zhe 50), SpectraVideo SV-603 VGA

In order to compile the example, type this command on the command line:

Linux

ugbc.coleco -O rom -o maths_relative_03.rom maths_relative_03.bas

Windows

ugbc.coleco.exe -O rom -o maths_relative_03.rom maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 128 (CPU 8502)

In order to compile the example, type this command on the command line:

Linux

ugbc.c128 -O prg -o maths_relative_03.prg maths_relative_03.bas

Windows

ugbc.c128.exe -O prg -o maths_relative_03.prg maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 128 (CPU Z80)

In order to compile the example, type this command on the command line:

Linux

ugbc.c128z -O prg -o maths_relative_03.prg maths_relative_03.bas

Windows

ugbc.c128z.exe -O prg -o maths_relative_03.prg maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 64, Commodore Executive 64

In order to compile the example, type this command on the command line:

Linux

ugbc.c64 -O prg -o maths_relative_03.prg maths_relative_03.bas

Windows

ugbc.c64.exe -O prg -o maths_relative_03.prg maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Dragon 32

In order to compile the example, type this command on the command line:

Linux

ugbc.d32 -O bin -o maths_relative_03.bin maths_relative_03.bas

Windows

ugbc.d32.exe -O bin -o maths_relative_03.bin maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Dragon 64

In order to compile the example, type this command on the command line:

Linux

ugbc.d64 -O bin -o maths_relative_03.bin maths_relative_03.bas

Windows

ugbc.d64.exe -O bin -o maths_relative_03.bin maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

MSX

In order to compile the example, type this command on the command line:

Linux

ugbc.msx1 -O rom -o maths_relative_03.rom maths_relative_03.bas

Windows

ugbc.msx1.exe -O rom -o maths_relative_03.rom maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Olivetti Prodest PC128, Thomson MO6

In order to compile the example, type this command on the command line:

Linux

ugbc.pc128op -O k7 -o maths_relative_03.k7 maths_relative_03.bas

Windows

ugbc.pc128op.exe -O k7 -o maths_relative_03.k7 maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

SEGA SC-3000

In order to compile the example, type this command on the command line:

Linux

ugbc.sc3000 -O rom -o maths_relative_03.rom maths_relative_03.bas

Windows

ugbc.sc3000.exe -O rom -o maths_relative_03.rom maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

SEGA SG-1000

In order to compile the example, type this command on the command line:

Linux

ugbc.sg1000 -O rom -o maths_relative_03.rom maths_relative_03.bas

Windows

ugbc.sg1000.exe -O rom -o maths_relative_03.rom maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

TRS-80 Color Computer, TRS-80 Color Computer 2

In order to compile the example, type this command on the command line:

Linux

ugbc.coco -O bin -o maths_relative_03.bin maths_relative_03.bas

Windows

ugbc.coco.exe -O bin -o maths_relative_03.bin maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

TRS-80 Color Computer 3

In order to compile the example, type this command on the command line:

Linux

ugbc.coco3 -O bin -o maths_relative_03.bin maths_relative_03.bas

Windows

ugbc.coco3.exe -O bin -o maths_relative_03.bin maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Thomson MO5

In order to compile the example, type this command on the command line:

Linux

ugbc.mo5 -O k7 -o maths_relative_03.k7 maths_relative_03.bas

Windows

ugbc.mo5.exe -O k7 -o maths_relative_03.k7 maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Thomson TO8

In order to compile the example, type this command on the command line:

Linux

ugbc.to8 -O k7 -o maths_relative_03.k7 maths_relative_03.bas

Windows

ugbc.to8.exe -O k7 -o maths_relative_03.k7 maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

ZX Spectrum +2, ZX Spectrum 128K, ZX Spectrum 48K

In order to compile the example, type this command on the command line:

Linux

ugbc.zx -O tap -o maths_relative_03.tap maths_relative_03.bas

Windows

ugbc.zx.exe -O tap -o maths_relative_03.tap maths_relative_03.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Any problem?

If you have found a problem trying to run this example, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO EXAMPLES