Author Topic: two different result..?  (Read 3014 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
two different result..?
« 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
Keep smile...

CADaver

  • Guest
Re: two different result..?
« Reply #1 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...

uncoolperson

  • Guest
Re: two different result..?
« Reply #2 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)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: two different result..?
« Reply #3 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-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: two different result..?
« Reply #4 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... :?
Keep smile...

CADaver

  • Guest
Re: two different result..?
« Reply #5 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?

sepperl

  • Guest
Re: two different result..?
« Reply #6 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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: two different result..?
« Reply #7 on: August 01, 2006, 10:55:08 AM »
Great explanation and welcome to the Swamp. :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: two different result..?
« Reply #8 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 (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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CADaver

  • Guest
Re: two different result..?
« Reply #9 on: August 01, 2006, 12:21:26 PM »
Great example Michael (go get some rest, dude), and sepperl (welcome aboard).

Andrea

  • Water Moccasin
  • Posts: 2372
Re: two different result..?
« Reply #10 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:
Keep smile...