Proton BASIC Compiler - Test for sign of a float


  • PicŪ Basic


  • Test for sign of a float

    To test whether a number is negative or positive you could locate the sign bit (it's in byte1 of a float).
    The sign bit is 1 for a negative float and 0 for positive.

    Code:
    Include "Proton18_4.Inc" ' Use the Proton board for the demo
    
    Dim Floatin As Float
    Dim Sign As Byte
    'testing for a negative or positive number
    Floatin = 0.123456
    Sign = Floatin.Byte1
    
    If Sign.7 = 1 Then
        Print At 1,1, "-ve"
    Else
        Print At 2,1, "+ve"
    EndIf
    End
    This is quite efficient code, the whole test occupies only 3 assembler instructions. It is however not as efficient as the simple Proton code of:
    If floatvar<0 then....
    which occupies just two assembler instructions.

    The direct access method is provided for your interest as an explanation of how float storage works.
  • Recent Activity

    david-48000

    Migrating a monitored alarm from copper to GSM.

    Thread Starter: david

    Hi All, I need to migrate a monitored house alarm from the present copper line to GSM so we can eventually adopt a fiber system that the telcos are...

    david Today, 11:26 Go to last post
    teo-16018

    I'm over the moon with joy. :-)

    Thread Starter: Les

    I just had to show you this, because I am over the moon with it. LOL I've just about finished procedures on the Proton 8-bit compiler so I am busy...

    teo Today, 15:03 Go to last post
    david-48000

    GPS speed logger for RC planes.

    Thread Starter: david

    Hi All, This is a simple project to allow the logging of RC plane speeds to help determine battery, motor and prop variables. The code is crude and...

    david Today, 03:44 Go to last post