Swordfish Basic Floating Point Math Problem.

I am working on another new project (surprise, surprise…) where I am measuring current of different devices and sending it to an LED matrix.

I am using a PIC18F13K50 microcontroller for the job and coding in Swordfish Basic. I was in the initial stages of testing the led matrix display and I ran some simple test code, like so:

dim AverageCurrent as Float
AverageCurrent = 153 / 100

All I wanted to do here was move the decimal place of the number 153, two places to the left to give me 1.53 – unfortunately though, my LED matrix would show 1.00 (I.E. it seemed to convert it to an integer). This was a problem! The variable ‘AverageCurrent’ was declared as a float, so why would it not give me a float answer?

Well, long story short, I ended up storing the number 153 in its own variable ‘MyFloat’ and then ran the math and everything worked out just fine:

dim MyFloat as Float
MyFloat = 1.53
AverageCurrent = MyFloat / 100

I.E. the number now stored in ‘AverageCurrent’ was 1.53 (not 1.00) So my best guess as to what swordfish basic is doing is it defaults any number you write in to an integer. I could be wrong, but I found a solution regardless!

Hope this comes in handy for those using SF Basic. (It will probably help me later on when I decide to do another project in SF Basic and forget to store floats in their own variable!)



I have been informed by one of our resident forum members Bitfogav that swordfish basic defaults to integers. If you want to use floating point, then you just need to add a decimal place and a zero to your number. So in my case, instead of using:

dim AverageCurrent as Float
AverageCurrent = 153 / 100

I would

dim AverageCurrent as Float
AverageCurrent = 153.0 / 100

do this:


Thanks Bitfogav!