I know I owe several of you responses, and thanks for being patient. I have just put in the most grueling two weeks working up the newest contribution, logarithms for Great Cow Basic.
I'm not looking for sympathy or slaps on the back, but let me say that this has been the singlemost difficult GCB project I've ever undertaken. The mathematics is trivial, but the side-effects in the actual implementation have driven me crazy for some fourteen very full days now. Anyway, I believe it's done, and would appreciate feedback.
Here's what we've got: base-2, base-e and base-10 logarithms, to two decimal places. Approximating transcendental functions like these always requires trade-offs, typically speed versus accuracy or speed versus memory usage. I tried to balance all three, giving a slight emphasis to accuracy. The results are very usable: all three logarithms are accurate to two decimal places, plus or minus 0.01 (1 one-hundredth) worst case, and that only happens very infrequently due to the table method and linear interpolation I used. This should be great for dealing with logarithmic phenomena like light, sound and pH.
For all three functions, the inputs are words and the outputs are words. As usual, the source code for the include file is heavily commented with lots of descriptions.
The three functions are log2(x), loge(x) and log10(x). I mentioned side-effects above. They drove me nuts! I wanted to call loge(x) its usual name, ln(x), but for whatever reason GCB won't allow it. I wasted over 24 hours in continuous testing before I figured that out. It's a good thing the compiler doesn't wear out from over-usage...Hugh, is "ln" some sort of reserved word? (That's an "ell-enn.")
Anyway, we'll call it loge(x) for now. Here's a demo program you can try. As usual, I'll attach the include file in the next posting.
;Demo:Printcommonlogarithmstotwodecimalplaces;ThomasHenry--5/1/2014;-----Configuration#chip 16F88, 8 ;PIC16F88 running at 8 MHz#config mclr=off ;reset handled internally#config osc=int ;use internal clock#include"Logarithms.h";-----Constants#define LCD_IO 4 ;4-bit mode#define LCD_RS PortB.2 ;pin 8 is LCD Register Select#define LCD_Enable PortB.3 ;pin 9 is LCD Enable#define LCD_DB4 PortB.4 ;DB4 on pin 10#define LCD_DB5 PortB.5 ;DB5 on pin 11#define LCD_DB6 PortB.6 ;DB6 on pin 12#define LCD_DB7 PortB.7 ;DB7 on pin 13#define LCD_NO_RW 1 ;ground the RW line on LCD;-----Variablesdimiasword;-----ProgramdirPortBout;alloutputstotheLCDfori=1to65535clsprint"log10("printi;printargumentprint")="locate1,0print2Places(log10(i));anditslogarithmwait2Snext;-----Subroutinessubprint2places(inp2p_argasword);printdecentlyformattedresults,to2decimalplacesdimvalStr,outStrasstringvalStr=str(p2p_arg)selectcaselen(valStr)case1:outStr="0.0"+valStrcase2:outStr="0."+valStrcase3:outStr=left(valStr,1)+"."+right(valStr,2)case4:outStr=left(valStr,2)+"."+right(valStr,2)endselectprintoutStrendsub
Last edit: Anonymous 2014-05-01
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-05-01
Here is the include file for the logarithm functions, base 2, base e and base 10.
OK. No sympathy but well done - a slap on the back! Very good.
We need to pull this all together into MATHS.H!!!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-05-01
Sounds good. Have you noticed that the sin(), cos(), tan(), sqrt(), log2(), loge() and log10() functions I've written all return values to two decimal places? This is on purpose. With the addition of the print routines that format the results with decimal points (see the various sample demo programs), we're starting to get a pretty good consistent set of fixed point decimal routines.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello all,
I know I owe several of you responses, and thanks for being patient. I have just put in the most grueling two weeks working up the newest contribution, logarithms for Great Cow Basic.
I'm not looking for sympathy or slaps on the back, but let me say that this has been the singlemost difficult GCB project I've ever undertaken. The mathematics is trivial, but the side-effects in the actual implementation have driven me crazy for some fourteen very full days now. Anyway, I believe it's done, and would appreciate feedback.
Here's what we've got: base-2, base-e and base-10 logarithms, to two decimal places. Approximating transcendental functions like these always requires trade-offs, typically speed versus accuracy or speed versus memory usage. I tried to balance all three, giving a slight emphasis to accuracy. The results are very usable: all three logarithms are accurate to two decimal places, plus or minus 0.01 (1 one-hundredth) worst case, and that only happens very infrequently due to the table method and linear interpolation I used. This should be great for dealing with logarithmic phenomena like light, sound and pH.
For all three functions, the inputs are words and the outputs are words. As usual, the source code for the include file is heavily commented with lots of descriptions.
The three functions are log2(x), loge(x) and log10(x). I mentioned side-effects above. They drove me nuts! I wanted to call loge(x) its usual name, ln(x), but for whatever reason GCB won't allow it. I wasted over 24 hours in continuous testing before I figured that out. It's a good thing the compiler doesn't wear out from over-usage...Hugh, is "ln" some sort of reserved word? (That's an "ell-enn.")
Anyway, we'll call it loge(x) for now. Here's a demo program you can try. As usual, I'll attach the include file in the next posting.
Last edit: Anonymous 2014-05-01
Here is the include file for the logarithm functions, base 2, base e and base 10.
OK. No sympathy but well done - a slap on the back! Very good.
We need to pull this all together into MATHS.H!!!
Sounds good. Have you noticed that the sin(), cos(), tan(), sqrt(), log2(), loge() and log10() functions I've written all return values to two decimal places? This is on purpose. With the addition of the print routines that format the results with decimal points (see the various sample demo programs), we're starting to get a pretty good consistent set of fixed point decimal routines.