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.

    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"
        Print At 2,1, "+ve"
    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.