- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have reason [it's complicated] to want to know the greatest common denominator of a pair of numbers, but when they are not integers which the (gcd) function requires. [For example, the greatest number that divides into both 4.0 and 2.5 is 0.5 -- that's what I'm looking for.]
The number of decimal places varies. So I'm finding the number of decimal places of the one with the most of them, raising 10 to that power as a factor [e.g. if one of them has 3 decimal places and the other no more than 3, use 10^3 = 1000], multiplying both numbers by that and (fix)ing them so they'll be integers, using (gcd) on those, and dividing the result back down by the same factor to get a decimal end result.
In trying things, I've come across a freakish thing. AutoLisp somehow doesn't like working in certain ways with numbers with 3 decimal places within a certain range, when the last decimal place is odd. I just coincidentally seem to have stumbled into that certain range by accident, but it may be [probably is?] true within other ranges of numbers, and with other numbers of decimal places.
The first few functions here return what you would expect, but note where it (fix)es to the next number down:
(fix (* 0.997 (expt 10 3)))
997
(fix (* 0.998 (expt 10 3)))
998
(fix (* 0.999 (expt 10 3)))
999
(fix (* 1.000 (expt 10 3)))
1000
(fix (* 1.001 (expt 10 3)))
1000 <-- first wrong one
(fix (* 1.002 (expt 10 3)))
1002
(fix (* 1.003 (expt 10 3)))
1002
(fix (* 1.004 (expt 10 3)))
1004
(fix (* 1.005 (expt 10 3)))
1004
.... same down-one error with all odd-third-decimal-place numbers 1.007 through 1.021 [omitted] ....
(fix (* 1.022 (expt 10 3)))
1022
(fix (* 1.023 (expt 10 3)))
1022 <-- last wrong one
(fix (* 1.024 (expt 10 3)))
1024
(fix (* 1.025 (expt 10 3)))
1025 <-- right again
(fix (* 1.026 (expt 10 3)))
1026
(fix (* 1.027 (expt 10 3)))
1027
Wondering about the (expt) function and possibly tiny processing errors, I checked way down the decimal places for it come up a hair short, but that didn't reveal anything:
(rtos (expt 10 3) 2 17)
"1000.000000000000"
Wondering about whether integer values could be the issue, I tried decimal values, but the same happens if I use (expt 10.0 3.0). Even forgetting (expt) entirely, it happens if I just straight-out use 1000 [and whether that's an integer here or a real number 1000.0]:
(fix (* 1.019 1000))
1018
And these parts of that work right:
(* 1.019 1000)
1019.0
(fix 1019.0)
1019
But there's something about the particular combination of functions....
It works right whenever that third decimal place is even, as apparent above, and when it's odd outside that particular range [within the short distances outside that I tried].
Does this happen for other people? Does anyone have any idea what could be causing such a thing, or whether or where other danger ranges might be, or how to prevent or compensate for the error?
Solved! Go to Solution.