(show&tell) The Floating Point Gotcha
Posted: Wed Apr 26, 2006 3:49 am
Here is a little reminder to remember to keep those integer and floating point functions straight.
I was looking at Steve Dekorte's (author of the Io language) blog the other day when i noticed some code he had written to calculate savings over a worktime. Oh goodie: more code to translate into newLISP! I had already finished translating most of my other sundry code into newLISP days ago. I was ready for more lisping.
But this almost ended up a sad story, you see. Besides being a terrible programmer (I'm a bricoleur), I'm also impatient. This led me to assume the problems I'd encountered were something personal between newLISP and me, so we decided to begin seeing other languages.
So, without further ado, here is the evil code that almost came between us:
Seasoned newLISPers will spot my mistake immediately, while others may have no clue what could be wrong with it. I tried and tried to figure out why, why, why this (obviously) correct script was not working!
newLISP kept telling me the answer was: "0 * 1 = 0". Clearly, my ignorance of contexts was at the bottom of this, I thought.
So on with this fool's quest I went till, coming to the end of my string, I decided to give up this "not OOP enough" newLISP :-(
Yes, still here friends, for as you already know, newLISP is a seductive mistress, indeed.
Getting back to the point of this (is there really a point to this?) communiqué, the integer<->float divide really bit me on this one. Just wanted to send up a little signal flare for any others who find themselves down this wrongway alley.
m i c h a e l
For the curious, the final code (using some ocaml-inspired constants defined in init.lsp) ended up looking like this:
Man, that's pretty.
P.S. Here is the original link.
I was looking at Steve Dekorte's (author of the Io language) blog the other day when i noticed some code he had written to calculate savings over a worktime. Oh goodie: more code to translate into newLISP! I had already finished translating most of my other sundry code into newLISP days ago. I was ready for more lisping.
But this almost ended up a sad story, you see. Besides being a terrible programmer (I'm a bricoleur), I'm also impatient. This led me to assume the problems I'd encountered were something personal between newLISP and me, so we decided to begin seeing other languages.
So, without further ado, here is the evil code that almost came between us:
Code: Select all
(context 'Person)
(setq
yearly-income 35000
saving-ratio .3
savings 0)
(define (work tax-rate inflation interest-rate)
(setq
$-value 1
tax-rate (or tax-rate .33)
inflation (or inflation .03)
interest-rate (or interest-rate .10))
(for (age 18 65)
(setq
income (- yearly-income (* yearly-income tax-rate))
savings (+ savings (* savings interest-rate))
savings (- savings (* savings inflation))
savings (+ savings (* income saving-ratio))
yearly-income (+ yearly-income (* yearly-income inflation))
$-value (- $-value (* $-value inflation))))
(silent
(println
(context) {: }
(floor savings) { * }
(format "%.2f" $-value) { = }
(floor (* savings $-value)))))
(context MAIN)
(new Person 'anna)
(anna:work)
newLISP kept telling me the answer was: "0 * 1 = 0". Clearly, my ignorance of contexts was at the bottom of this, I thought.
So on with this fool's quest I went till, coming to the end of my string, I decided to give up this "not OOP enough" newLISP :-(
Yes, still here friends, for as you already know, newLISP is a seductive mistress, indeed.
Getting back to the point of this (is there really a point to this?) communiqué, the integer<->float divide really bit me on this one. Just wanted to send up a little signal flare for any others who find themselves down this wrongway alley.
m i c h a e l
For the curious, the final code (using some ocaml-inspired constants defined in init.lsp) ended up looking like this:
Code: Select all
(context 'Person)
(setq
yearly-income 35000
saving-ratio .3
savings 0)
(define (work tax-rate inflation interest-rate)
(setq
$-value 1
tax-rate (or tax-rate .33)
inflation (or inflation .03)
interest-rate (or interest-rate .10))
(for (age 18 65)
(setq
income (-. yearly-income (*. yearly-income tax-rate))
savings (+. savings (*. savings interest-rate))
savings (-. savings (*. savings inflation))
savings (+. savings (*. income saving-ratio))
yearly-income (+. yearly-income (*. yearly-income inflation))
$-value (-. $-value (*. $-value inflation))))
(silent
(println
(context) {: }
(floor savings) { * }
(format "%.2f" $-value) { = }
(floor (*. savings $-value)))))
(context MAIN)
(new Person 'anna)
(anna:work)
(new Person 'betty)
(betty:work .25 0 .07)
P.S. Here is the original link.