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!)

**Update!**

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!

Yes that can be very annoying if you don’t know about how Swordfish deals with floating-point, you are correct in saying Swordfish defaults any maths to integer.

Another way of letting Swordfish know that you want to do a math calculation as a floating-point is by adding a .0 at the end of the value for example:

`AverageCurrent = (153.0 / 100)`

Thanks Bitfogav!

How would I have found out about that, I had a read through the SF manual but couldn’t find anything, I also had a very brief look at sfcompiler.co.uk and could’t find it there either.

I’ll update this post to include your answer š

There is a statement in SF manual relating to the compiler and arithmetic, I know the statement relates to Constant declarations but the same also applies to Varibles. I guess this is why it can be easily missed.

QuoteSwordfish will compute a constant declaration such as const Value = 5 / 1024 using integer arithmetic, resulting in Value being equal to zero. This is because both 5 and 1024 are ordinal values. If you want to force the type of Value to floating point, one or more of the literals in the expression should be made floating point, for example, const Value = 5.0 / 1024 or const Value = 5 / 1024.0Thanks for posting my answer Brad š

sir,

did swordfish allow simulation with out pic hardware?what is the advantage over learning basic over c?which is more useful?

Sorry for the late reply Baiju. Swordfish Basic does not have an inbuilt simulation tool, but you could compile the source code and then import this into a simulation package (I have used proteus to do this). I have found Basic and C to be very similar but Basic is much nicer to code in. For example, in C a for loop would look something like this:

for(x = 0; x <8; x++){

run some code here…

}

but the same code in basic would look like this:

for x = 0 to 7

run some code here…

next

I find they are just as useful as each other, but I prefer basic.