Menu

Single Precision to Long

2021-08-13
2021-08-25
  • Clint Koehn

    Clint Koehn - 2021-08-13

    I needed to convert a single precision number to a integer value. This is what I came up with. You lose a little precision but that happens anyway when dealing with integer math.

    sub SingleToLong(sgn as Byte, fnx as long, prec as byte)
      '-----------------------------------------------------
      ' sgn - the sign is returned in this variable
      ' fnx - single precision in, long variable out
      ' prec - number of digits of precision (e.i. 3 = 1000)
      '-----------------------------------------------------
    
      dim fractional as Word
      dim mantissa as Byte
    
      fnx = FnLSR(fnx, 7) 'make less precise to fit in a word variable
      fractional = fnx & 0xFFFF
      fnx = FnLSR(fnx, 16)
      mantissa = fnx & 0xFF
      sgn = FnLSR(fnx, 8) ' 1 is negative, 0 is positive
    
      fnx = Power(10, prec)
    
      fnx = (([long]fractional * fnx) / 0x10000) + fnx
    
      if mantissa < 127 then
        'fractional - divide
        mantissa = 127 - mantissa
        fnx = (fnx / Power(2, mantissa)) + 1
      else
        'whole number - multiply
        mantissa = mantissa - 127
        fnx = (fnx  * Power(2, mantissa)) + 1
      end if
    
    end sub
    

    Later,
    Clint

     

    Last edit: Clint Koehn 2021-08-13
  • jackjames

    jackjames - 2021-08-13

    At first glance it seems OK to me.

     
  • Anobium

    Anobium - 2021-08-13

    @Clint. A great function.

    Also, I have invited you to review the Real number support thread. This thread is for Developers only and as you will see the state of the Real number implementation means it is not ready for 'prime time' sharing. Please review the thread and post your thoughts and comments as you may be able to help us. :-)

    https://sourceforge.net/p/gcbasic/discussion/579127/thread/b867317011/#182a/026a

    @Others. This is a closed thread as the development is not stable.

     
  • Clint Koehn

    Clint Koehn - 2021-08-13

    I will be glad to look at it.

    I did make a mistake that I have corrected. I divided by 0xFFFF and it should have been 0x10000.

    Later,
    Clint

     
  • stan cartwright

    stan cartwright - 2021-08-13

    What will it do when sorted out? Will we be able to use floats?
    Will we be using #define pi like arduino code does? Just interested. I'm rubbish at hard sums.
    Found this https://en.wikipedia.org/wiki/Single-precision_floating-point_format

     

    Last edit: stan cartwright 2021-08-13
  • Clint Koehn

    Clint Koehn - 2021-08-13

    I'm using this for retrieving a single precision value from a sensor. I really need to rename my variables, they aren't named right. The fractional name should be mantissa and the mantissa should be exponent. :( I too am a little new at the float/single/double precision game. I'm too use to integer math on these microcontrollers I program with gcbasic. It has worked well for me.

    Later,
    Clint

     
    • stan cartwright

      stan cartwright - 2021-08-13

      @Clint - How does gcb do sine and cos trig?
      I don't know but it works and usually would be floating point but isn't???
      I have seen arduino progs for devices I want to convert to gcb but they for some reason use pi. They are also in c so converting to gcb not feasible.
      Ashamed to say I don't understand integer in gcb and it's sign if over 32768
      ie half a word. It becomes +
      Doh! is my response.. so I'm missing a lot of basic computer math... but I get by.
      Nice work sir though.. and hope it gets sorted.
      I get by with what I understand, not much... but "fun" using what I know.
      This reminds me of "o" level math in school at 15 and "o" level chemistry.
      What's covalent bonds... what's a mantissa??
      regards stan

      Edit I meant sin - cos is like analogue but is digital... how they do it?

       

      Last edit: stan cartwright 2021-08-13
  • Clint Koehn

    Clint Koehn - 2021-08-19

    This probably isn't the right place to explain this but...
    gcb does sin and cos by lookup tables. Integer math isn't really that hard. You do have to know what the min and max of the number you expect to end up with, and then you can make it fit in a long variable with a decimal point assumed. (e.i. 1235234 = 123.5234) 4 decimal points assumed. Say for instance you need to divide x by 1.32, you would first ([long]x * 10000) / 132. Your answer would have an assumed 2 decimal places. 90/1.32 = (90 * 10000) / 132 = 6818 = 68.18

     
  • stan cartwright

    stan cartwright - 2021-08-19

    Thanks for replying. There's a dev only thread which is forbidden.
    lack of decimal point vars and arithmetic was posted about years ago.
    my first thoughts were multiply the numbers by multiples of 10 to make say 3.321
    a whole number then after calc divide to get the decimal value back. Very naïve of me.

    I use the trig functions but never looked at the include lib so surprised it used tables.

    Good luck with the development.

     
    • Anobium

      Anobium - 2021-08-19

      Summarising the Dev thread. Floats are not ready for general release or support. It would be a support nitemare.

      😐

       
  • stan cartwright

    stan cartwright - 2021-08-19

    Hi Anobium. I saw a speed test of uno 328 and lgt 328 on youtube which calculated prime numbers.
    Later the guy did another video saying someone mentioned his arduino c program used floats which seemed to be default unless the user says vars aren't floats.
    I would imagine floats to be difficult to implement in gcb and we've got by without them so far.
    I thought what I'd use pi for. Got by without it using gcb trig for displaying dials and even some math for 2 rangefinders to work out angles and distance.
    What would others use floats for?
    The scale function is one of my favourite routines, which came from someone asking for a gcb answer to the arduino map function. I have seen posts that used it so it was worth the effort to create, it has many uses.

     
    • Anobium

      Anobium - 2021-08-19

      I cannot comment on INTs v FLOATs and the timeline to the implement within Great Cow BASIC. Only Hugh knows.

      Why use floats? a really simple use case is a sensor that uses a float to store the sensor value. Floats will aid sensore integration.

      Scale/map. 😀

       
  • stan cartwright

    stan cartwright - 2021-08-19

    Hi Anobium. I saw a speed test of uno 328 and lgt 328 on youtube which calculated prime numbers.
    Later the guy did another video saying someone mentioned his arduino c program used floats which seemed to be default unless the user says vars aren't floats.
    I would imagine floats to be difficult to implement in gcb and we've got by without them so far.
    I thought what I'd use pi for. Got by without it using gcb trig for displaying dials and even some math for 2 rangefinders to work out angles and distance.
    What would others use floats for?
    The scale function is one of my favourite routines, which came from someone asking for a gcb answer to the arduino map function. I have seen posts that used it so it was worth the effort to create, it has many uses.

     
  • Anobium

    Anobium - 2021-08-19

    ditto

     
  • mmotte

    mmotte - 2021-08-19

    Certainly the first example of need is when I was working on the BME280 sensor in 2018-19. It required the library to access the calibration data and perform quite complicated manipulations on the sensor data coming out to get the humidity, abs pressure, and temperature. Floating point was used by one of the examples in the data sheet. The data sheet also showed an signed integer solution but it was signed long 32bit not the signed 16 bit that gcb has.

    After months of trying various things, someone on this forum showed me how to make signed 32 bit long routine for mult and div. Thank you who ever that was!. That got the library to work.

    Anobium are the floating point routines in a library file? or in the low level? or do i need a different version compiler?

    Thanks
    mike

     
    • Anobium

      Anobium - 2021-08-20

      @mmotte As a developer read thread and post here: GCB Developers Only: FLOATS - NOT TO BE USED FOR PROJECTS - Recommended for evaluation and testing only

      They are NOT ready for projects. I will post the current status here to keep non-devs up to date.


      Current Status - not to be used on Projects

      Use this post responses to track the current status

      Issues - all deemed serious enough NOT to release for general release.

      • Issues with simple maths are causing improper maths and lock ups of mcu.
      • Simple subtraction is not returning proper results
      • Floats cannot be passed as parameters to methods.
      • String constants used to assign values to a double that have trailing zeros does not generate valid asm.
      • Advanced variables will not accept full range of potential values
      • Rotate usage needs clarification
      • Negate usage needs clarification
      • INT() and ROUND() clarification
      • Help is here: https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/variablesadvanced.adoc
       

      Last edit: Anobium 2021-08-20
    • stan cartwright

      stan cartwright - 2021-08-20

      @mmotte, I've looked for hardware solutions before the device was implemented in gcb and seen arduino code that used define pi 3.xxxxx.
      As Anobium said, floats are used for data from some devices.
      Finding basic code for devices is often difficult and if found often tricky to convert.

       
  • stan cartwright

    stan cartwright - 2021-08-25

    @mmotte . I want to make a self balancing robot and have a gyro supported in gcb compass demos.
    I didn't get far but all rpi/arduino code for the gyro/bot is floating point.
    Will revisit.

     

Log in to post a comment.