TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Andrea on July 31, 2006, 04:07:45 PM
-
(setq t1 2345.0
t2 5280.0)
(rtos t1 2 13)
(rtos t2 2 13)
t1 = "2345"
t2 = "5280.000000000001"
? ? :-o
-
Command: (setq t2 5279.00000000000000)
5279.0
Command: (rtos t2 2 13)
"5278.999999999999"
Command: (setq t2 5280.0000000000000000000)
5280.0
Command: (rtos t2 2 13)
"5280.000000000001"
Command: (setq t2 5281.0000000000000000000)
5281.0
Command: (rtos t2 2 13)
"5281"
double hmmm...
-
double hmmm...
i think that's the problem
thought this was fun, till it ate up my computer (save everything you have open if you try this)
-
;; Pad.lsp CAB 07/13/06
;; Pad right with 0 to precision plc
;; No Rounding!
(defun pad (str plc / sp)
(setq tmp (reverse (vl-string->list str)))
(if (not (vl-position 46 tmp))
(setq tmp (cons 46 tmp))
)
(cond
((= (vl-position 46 tmp) plc))
((< (vl-position 46 tmp) plc)
(repeat (- plc (vl-position 46 tmp))
(setq tmp (cons 48 tmp))
)
)
(t
(repeat (- (vl-position 46 tmp) plc)
(setq tmp (cdr tmp))
)
)
)
(vl-list->string (reverse tmp))
)
(defun c:test ()
(setq t1 2345.0
t2 5280.0
)
(print (pad (rtos t1 2 13) 13))
(print (pad (rtos t2 2 13) 13))
(princ)
)
8-)
-
thanks CAB...
but don't not understand why we need to do all this to have the good result... :?
-
thanks CAB...
but don't not understand why we need to do all this to have the good result... :?
Andrea, do you realize just how tight 13 decimal places is? To put it in perspective thats a little over half a mile per LIGHTYEAR or 0.004 mm per the circumference of the Earth. Just how "GOOD" a result do you really need?
-
Hello !
Representing numbers is a general problem !
When you store a number as double (64-Bit) you can only represent 2^64 different numbers.
But if you imagine, that the amount of numbers is infinite, you see that you cannot display ALL possible numbers.
-> not all numbers are available, only a 'few' (2^64)
When a number is not available, then the closest to it will be used.
Example:
5279 is not possible -> 5278.999999... is used.
You see, a computer is not PERFECT !
Calculations on computer are only a approximation !
This often causes rounding errors, especially when you calculate big numbers.
-
Great explanation and welcome to the Swamp. :-)
-
... don't not understand why we need to do all this to have the good result... :?
It's the nature of floating point numbers as represented by computers: some numbers are approximations and cannot be stored / represented exactly. Observe --
63 / 9 = 7
6.3 / 0.9 = 7
0.63 / 0.09 = 7
0.063 / 0.009 = 7.000000000000001
0.0063 / 0.0009 = 7
0.00063 / 0.00009 = 7.000000000000001
0.000063 / 0.000009 = 7.000000000000001
0.0000063 / 0.0000009 = 7.000000000000001
0.00000063 / 0.00000009 = 7.000000000000001
0.000000063 / 0.000000009 = 7.000000000000001
0.0000000063 / 0.0000000009 = 7.000000000000001
0.00000000063 / 0.00000000009 = 7.000000000000001
0.000000000063 / 0.000000000009 = 7
0.0000000000063 / 0.0000000000009 = 7
0.00000000000063 / 0.00000000000009 = 7
0.000000000000063 / 0.000000000000009 = 7
IOW, code accordingly (eg use the equal function where necessary). See link (http://en.wikipedia.org/wiki/Floating_point) (one of thousands) that details floating point mechanics and problems.
Code to generate the numerical pyramid above --
( (lambda ( number divisor / rset tostring )
(defun rset ( text padding width )
(substr
( (lambda ( )
(while
(<
(strlen (setq padding (strcat padding padding)))
width
)
)
(setq text (strcat padding text))
)
)
(- (strlen text) (1- width))
)
)
(defun tostring ( number ) (rtos number 2 15))
(repeat 16
(princ
(strcat
(rset (tostring number) " " 17)
" / "
(substr
(strcat (tostring divisor)
" "
)
1
17
)
" = "
(tostring (/ number divisor))
"\n"
)
)
(setq
number (/ number 10.0)
divisor (/ divisor 10.0)
)
(princ)
)
)
63.0
9.0
)
Edit: Looks like another post was made before I got to post this that covers some of the same ground. I'm too tired to care.
-
Great example Michael (go get some rest, dude), and sepperl (welcome aboard).
-
Hi sepperl, and welcome to the swamp.
Thanks all of you guys....i will try to study a little more.. :laugh: