Floating point problem
Rsadeika
Posts: 3,837
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:
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 seenrx()
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
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 likeprint
andinput
are easy to use. Get the code working with the default serial port, and then if you need to use another one you can useopen
withsendrecvdevice
to access the other serial port.I am using the jm_fullduplexserial.spin2, which is getting me closer to what I would like.
Ray