TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Andrea on July 31, 2006, 04:07:45 PM

Title: two different result..?
Post by: Andrea on July 31, 2006, 04:07:45 PM
Code: [Select]
(setq t1 2345.0
      t2 5280.0)

(rtos t1 2 13)
(rtos t2 2 13)

t1 = "2345"
t2 = "5280.000000000001"

? ?   :-o
Title: Re: two different result..?
Post by: CADaver on July 31, 2006, 04:29:59 PM
Quote
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...
Title: Re: two different result..?
Post by: uncoolperson on July 31, 2006, 05:12:30 PM
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)
Title: Re: two different result..?
Post by: CAB on July 31, 2006, 07:18:26 PM
Code: [Select]
;;  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))
)




Code: [Select]
(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-)
Title: Re: two different result..?
Post by: Andrea on August 01, 2006, 08:50:49 AM
thanks CAB...

but don't not understand why we need to do all this to have the good result... :?
Title: Re: two different result..?
Post by: CADaver on August 01, 2006, 09:51:11 AM
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?
Title: Re: two different result..?
Post by: sepperl on August 01, 2006, 10:51:39 AM
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.
Title: Re: two different result..?
Post by: CAB on August 01, 2006, 10:55:08 AM
Great explanation and welcome to the Swamp. :-)
Title: Re: two different result..?
Post by: MP on August 01, 2006, 11:03:10 AM
... 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 --

Code: [Select]
(   (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.
Title: Re: two different result..?
Post by: CADaver on August 01, 2006, 12:21:26 PM
Great example Michael (go get some rest, dude), and sepperl (welcome aboard).
Title: Re: two different result..?
Post by: Andrea on August 01, 2006, 12:51:58 PM
Hi sepperl, and welcome to the swamp.

Thanks all of you guys....i will try to study  a little more.. :laugh: