View previous topic :: View next topic |
Author |
Message |
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Wed Dec 31, 2014 6:56 pm Post subject: for the fist of january it could be a hard question about MO |
|
|
Hello all
with exel I have :
=MOD(716.744;360) and the result is 356.744
I try :
$regfile = "M328pdef.dat"
$crystal = 16000000
Config Clockdiv = 1
$hwstack = 128
$swstack = 64
$framesize = 128
$baud = 19200
Config Submode = New
Dim J As Single , B As Single
J = 716.744
B = J Mod 360
Print B
And I have an data type error
why ?
Many thanks for your answers
JP
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Wed Dec 31, 2014 7:39 pm Post subject: |
|
|
I know that is not answear to Your question but this is semi solution.
Code: |
$sim
$regfile = "M328pdef.dat"
$crystal = 16000000
Config Clockdiv = 1
$hwstack = 128
$swstack = 64
$framesize = 128
$baud = 19200
Config Submode = New
Cls
Dim J As Single , B As Single
Dim C As Word , D As Word
J = 716.744
C = J
B = J - C
D = C Mod 360
B = B + D
Lcd B
End |
|
|
Back to top |
|
|
weec
Joined: 13 Oct 2013 Posts: 7
|
Posted: Wed Dec 31, 2014 10:24 pm Post subject: |
|
|
Hello Duval JP try this:
$regfile = "M328pdef.dat"
$crystal = 16000000
Config Clockdiv = 1
$hwstack = 128
$swstack = 64
$framesize = 128
$baud = 19200
Config Submode = New
Dim J As Single
Dim B as Word
J = 716.744
B = J 'make a word (=716)
B = J Mod 360 ' Remainder = 356
Print B
In Bascom : Mod Calculates the remainder in numbers are Dword, Word, Integer or Byte.
Happy new Year
Werner |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu Jan 01, 2015 11:34 am Post subject: |
|
|
Many thanks for all "1 January workers !"
Yes, unfortunately Mod is working only with integer with Bascom basic
so
I do like and it gives the same results as Excel
$regfile = "M328pdef.dat"
$crystal = 16000000
Config Clockdiv = 1
$hwstack = 128
$swstack = 64
$framesize = 128
$baud = 19200
Config Submode = New
Dim J As Single , B As Long , C As Long
J = 716.744
J = J * 1000 'remove the decimal part
C = J ' J now is integer number
B = C Mod 360000
Print "B;" ; B
J = B / 1000
Print "J:" ; J
And now I close the computer for the day !
JP |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Jan 01, 2015 11:02 pm Post subject: |
|
|
A quick and dirty way of doing mod on singles, if you need it just to preserve things like angular measure going over 360 during computations, is to do this:
Code: |
dim j as single
j = 716.744
while j > 360
j = j - 360
wend
|
Using While is better than using If, since it handles also values over 720
Of course its terribly inefficient for big j, but for this type of case, its good enough.
And you can wrap it in a sub
Code: |
Sub ModSingle(p as single,m as single)
while p > m
p = p - m
wend
end sub
|
You can even extend this to handle negative values, which even Excel wont handle, but are very useful for this type of calculation. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Fri Jan 02, 2015 11:51 am Post subject: |
|
|
Hello Andrian,
Whaooooo! Nice !
may be you know I try to make a new astronomic clock, I'm starting from the works of Jan Meeus a Belgium astronomer .
I have to translate a lot of equations in Bascom-basic. I have a lot of MOD, and Trig calculation.
But I'm a little bit crazy, I know and my wife confirms
Your solution is simple and perfect for that.
Many thanks
JP |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Fri Jan 02, 2015 11:17 pm Post subject: |
|
|
I had heard of Jan Meeus, but I must admit that I thought he was much earlier than the 1920s. Certainly he did some good work on astronmical calculations. Have fun translating all those complex calcs to Bascom ! _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Jan 02, 2015 11:18 pm Post subject: |
|
|
actually, MOD is supported for singles. you need to use single variables for the operands.
the current version uses a different algo than excel/vba. if i remember correct, you will get various values for various MS basics from GW, QB, VB, to Excel.
Josef has written the trig lib and we discussed the MOD in the past. Since excels/VBA algo is the most common one, i have changed the fpmod function to reflect this.
It is simple to change :
- open fp_trig.lib
- remark the 2 calls to _fpround like
; rcall _FPRound
^remark
- save and compile the lib using lib manager _________________ Mark |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Fri Jan 02, 2015 11:49 pm Post subject: |
|
|
Hi Mark,
I just tried this in Bascom 2.0.7.7, but got an error 61:
Error : 61 Line : 23 Label not found [_GETFRAMESPACE2XZ ] , in File : G:\adrian\MyDocuments\Languages\AVR\Test\modtest.bas
Error : 61 Line : 23 Label not found [_GETFRAMESPACE2XZ ] , in File : G:\adrian\MyDocuments\Languages\AVR\Test\modtest.bas
Error : 61 Line : 23 Label not found [_RELEASEFRAMESPACE ] , in File : G:\adrian\MyDocuments\Languages\AVR\Test\modtest.bas
Error : 61 Line : 23 Label not found [_RELEASEFRAMESPACE ] , in File : G:\adrian\MyDocuments\Languages\AVR\Test\modtest.bas
Code: |
$regfile = "M328pdef.dat"
$crystal = 16000000
Config Clockdiv = 1
$hwstack = 128
$swstack = 64
$framesize = 128
$baud = 19200
Config Submode = New
Dim J As Single , B As Single
Dim m as single
J = 716.744
m = 360.0
B = j Mod m
print b
End
|
_________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sat Jan 03, 2015 12:23 am Post subject: |
|
|
works for me, but i use 2078. _________________ Mark |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Sat Jan 03, 2015 3:53 am Post subject: |
|
|
Confimed it works with version 2.0.7.8.
Thanks Mark, that is a useful addition, especially with the rounding removed.
But your normal distribution only includes the .lbx file. not the .lib file. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
|