Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Math in basic

Status
Not open for further replies.

be80be

Well-Known Member
Basic does math from left to right? Right

so if I do say X is a Float

X = (1023/403)-1 //I should get 1.538 in X


I get a 1

Now do the rest

X = (1023/403)-1 //I should get 1.538 in X
x = (10000/X) // should be 6501.950

I get a dang 10000 for X why is this is this it's not giving the .538 it's rounding to a 1
 
Yeah but..... (There's always a but)....

It's integer math and it truncates.

So in your first line, 1.538 becomes 1. The fractional part disappears.

If you can arrange all the multiplies first, then the divides, you may not loose the resolution.
 
This make swordfish handle the math right
Code:
X = (1023.0/403)-1 //I should get 1.538 in X
x = (10000.0/X) // should be 6501.950

Adding a decimal 0 tell the compiler i'm using floating-point math Thanks to Jerry for pointing that out.
 
That little bit of math let's the PIC become a ohm meter It's reading are very good side by side with VOM they read the same.

[video=youtube_share;vENFR3rZw3M]http://youtu.be/vENFR3rZw3M[/video]
 
I'm sure it would I'm not the best writer I was going to post how it worked. I thought it wasn't doing the math with my first code but Jerry said it was giving the right results in mpsim.

So what was happening was I was using a float when i output to the lcd it rounded it off. The block of code in post 3 fixed that.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top