Shop OBEX P1 Docs P2 Docs Learn Events
Floating point problem — Parallax Forums

Floating point problem

I am having a problem trying to print a floating point number. In the code snippet, I am receiving, from the activity board, a floating point number. As a test I want to capture and print the number that I received, but at the moment all I get is : 0.0000E-38 . Not sure how to covert this to something understandable. I am using jm_fullduplexserial.spin2, which does not contain a Pub for dealing with floating point numbers, as far as I can tell. Since the code is Spin, not sure if I am interpreting the Pubs correctly.

Ray

else if inBuff = "solar array" then

            abwx.str("solar array")  '' Send request to Activity board
            abwx.tx(10)
            abwx.tx(13)
            pausems 150
            inData# = abwx.rx()  '' Get the floating point data from Activity board


            pausems 10
            print inData#

Comments

  • Is there a formatted print function or a rounding option?
    Floating point numbers are an infamous mess for things like this. 0.0E-38 basically means "something pretty close to zero." Unfortunately, the floating point math routines aren't quite smart enough to realize that what you want to see here is zero. Floats get especially dangerous when you're working with powers of ten and things like money because the fraction 1/10 is infinitely repeating in binary, as 1/3 is in decimal, and so it tends to get truncated and not rounded properly unless you deliberately request rounding. When I was a lot younger there was an entire college course called Finite Math which was required if you wanted a computer science degree addressing issues like this, but it hasn't been required for a degree since 1990 or so at most universities.

  • After looking in the flexbasic docs,closer, it shows this:

    '' this function returns a string and takes a float and string as parameters
       function f$(a#, b$)
         ...
       end function
    
       '' this function also returns a string from a float and string
       function g(a as single, b as string) as string
         ...
       end function
    

    both of these functions are requiring two values, not sure how to work with these functions, considering I have , inData# = abwx.rx(), coming in. I guess I need a flexbasic expert to explain to me, how to use these functions, and will it get me what I am looking for..

    Ray
    Thanks

  • @Rsadeika What makes you think that inData# = abwx.rx() is the way to read a floating point number? In every serial object I've ever seen rx() reads a single character and returns its (integer) ASCII value.

  • Got it, so that means that I need a serial object that reads more than a single character. So, at the moment, since their are no serial objects within flexprop or spin/spin2, is their another way to approach this problem?

    I guess I might have to try reading it as a string, but I do not remember any serial objects that have rxString(). I might be mistaken, probably am.

    Ray

  • @Rsadeika said:
    Got it, so that means that I need a serial object that reads more than a single character. So, at the moment, since their are no serial objects within flexprop or spin/spin2, is their another way to approach this problem?

    You're better off using BASIC statements like input to read floating point. In fact I would probably do all the I/O using BASIC, myself, because things like print and input are easy to use. Get the code working with the default serial port, and then if you need to use another one you can use open with sendrecvdevice to access the other serial port.

    dim ser2 as class using("SmartSerial.spin")
    ser2.start(RXPIN, TXPIN, 0, 115_200)   ' substitute appropriate pins and baud rate here
    open SendRecvDevice(@ser2.tx, @ser2.rx, nil) as #3
    
  • RsadeikaRsadeika Posts: 3,837
    edited 2023-12-05 23:03

    I am using the jm_fullduplexserial.spin2, which is getting me closer to what I would like.

    Ray

    dim abwx as class using "jm_fullduplexserial.spin2"    '' AB WX Activity board
    abwx.start(28,29,0,115200)
    open SendRecvDevice(@abwx.tx, @abwx.rx, nil) as #4
    
    '' Small snip of code.
        else if inBuff = "solar array" then
                do  '' This is for a test run.
                print #4 using "!"; "a"    '' If I add a full string, this falls apart.         
                input #4,  inData           
                print using "##.# Volts"; inData            
                pausems 2000  '' Slow it down so I can see and capture.
                loop
    
    
    This is what I am receiving.
    
    
    
      solar array
    
    
       a Volts      '' Must be a timing issue                                                   
     ??a Volts     ''  Must be a timing issue.                                                               
    0.15 Volts    '' The following are the correct values.                                                                  
    0.11 Volts                                                                      
    0.13 Volts                                                                      
    0.13 Volts                                                                      
    0.13 Volts                                                                      
    0.11 Volts 
    
Sign In or Register to comment.