math

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
rverm
Posts: 135
Joined: 18 Nov 2007 04:16
Location: sunny florida

math

#1 Post by rverm » 22 Apr 2008 18:53

i was looking at the math results in the debugger for a pow(50,3). the results should be 12,500 but they are 59xxx. the lcd shows the same value. where can i see the actual value. or is it a binary value that as to be converted to decimal to display the correct value.

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: math

#2 Post by zristic » 23 Apr 2008 08:15

If you are storing the result into a word variable, then it will be 59464.
You have to store it into something larger than word, longword would be enough:

Code: Select all

dim ggg as longword

main:
...
  ggg = pow(50, 3)
...

end.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 23 Apr 2008 08:16

Hi,

What version of compiler you use ?

I use 5.0 and at me, in simulator, everything it's OK.

Code: Select all

program Test_Pow

dim res as float

main:
  res = pow(50.0,3.0)
  nop
end.
result is 1.250000E+005
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

rverm
Posts: 135
Joined: 18 Nov 2007 04:16
Location: sunny florida

#4 Post by rverm » 24 Apr 2008 00:38

tried the dim ggg as longword
w0 comes out as 59463
same as the word
it also shows up on the lcd as 59463
tried the dim ggg as float
it comes up with a odd number but no 125000
the comipler is 5.0
just want to be able to do math with these chips
the number on ggg as real,float is 1207182332
ok ggg comes 124999
canged the wordtostr to longwordtostr and everything comes to the lcd correctly.

rverm
Posts: 135
Joined: 18 Nov 2007 04:16
Location: sunny florida

#5 Post by rverm » 24 Apr 2008 21:13

hey thanks for your help. as you can i'm not to familiar with mb programming.
my job keeps me away from this stuff.
however i would like to make a mobile robot that follows poeple around, but i havnt been able to find a thermal sensor array 15x15. to follow someone's heat signature. hard to find sensor.

Pensador_scs
Posts: 1
Joined: 15 Sep 2008 12:31

Math question

#6 Post by Pensador_scs » 15 Sep 2008 14:33

Hi rverm,

I have ever math problems. I'm begginer programmer for dspic, but I can't understand some cases that happening to me.

For example, I need to store a Coeficient in EEPROM. There is 'Coef' variable as float. My Coef is ever greater than 0 and under '1'.

So, to write this value to eeprom first I convert to Longint multiplying for 100000000, to use all precision os Coef. Then I write using Loword and HiWord commands.

After writted I need to reload this varible, reading word by word, then multiplying for 0.00000001

I appreciate all idea to simplify this rotine, because there is to slow and
need many variables only to do it.


Thanks!

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: Math question

#7 Post by zristic » 17 Sep 2008 15:49

Try this:

Code: Select all

program test


dim  Ptr as ^byte
dim  FloVar as float

main:
  FloVar = 1.2345 ' = 0x3F9E 0419
  
  Ptr = @flovar
  
  latb = Ptr^ ' latb = 0x19 
  Ptr = Ptr + 1
  
  latb = Ptr^ ' latb = 0x04
  Ptr = Ptr + 1
  
  latb = Ptr^ ' latb = 0x9E
  Ptr = Ptr + 1
  
  latb = Ptr^ ' latb = 0x3F
  Ptr = Ptr + 1
end.
This example shows how to extract bytes from a float variable by using pointers. It can be easily modified to write the float variable to eeprom.

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”