Author Topic: Challenge - Real number precision?  (Read 5317 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Challenge - Real number precision?
« on: November 02, 2006, 07:37:47 PM »
If you get user input for a real number, how would you determine the precision of the number entered?
Examples of  user enters:
0.007   
1.05
.1
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.

sinc

  • Guest
Re: Challenge - Real number precision?
« Reply #1 on: November 02, 2006, 07:40:38 PM »
If you get user input for a real number, how would you determine the precision of the number entered?
Examples of  user enters:
0.007   
1.05
.1

How about the precision of:

10
579000
579000.0

Are those included in the challenge?   (not that I'll have time to work on it..)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge - Real number precision?
« Reply #2 on: November 02, 2006, 07:52:00 PM »
Sure, and I wish you had the time. :-)
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.

Derwin

  • Guest
Re: Challenge - Real number precision?
« Reply #3 on: November 02, 2006, 08:30:16 PM »
my solution:

Code: [Select]
; ----------------------------------------------------------------------------
; memeeng 031106 returns precision of given number
;
(defun whatprec ( VAL )
  (if (= 0 (- VAL (fix VAL)))
    0
    (progn
      (setq SVAL (rtos VAL 2))
      (setq SVAL (substr SVAL (+ 2(vl-string-search "." SVAL))))
      ;strip off RH '0'
      (while (= "0" (substr SVAL (strlen SVAL) 1))
(setq SVAL (substr SVAL 1 (1-(strlen SVAL)) )) )
      (strlen SVAL)
    );endPROGN
  );endIF
)

Derwin

  • Guest
Re: Challenge - Real number precision?
« Reply #4 on: November 02, 2006, 08:37:37 PM »
hmm, one thing:

the result will only go as high as "LUPREC" will allow, ACAD2004=8 places.
  ie: (setvar "LUPREC" 8)

maybe for deeper precision a set of comparrisons with .000000001, .0000000001 etc would work better..


MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Challenge - Real number precision?
« Reply #5 on: November 02, 2006, 08:57:55 PM »
In my opinion you will not be able to determine the input precision of values obtained via getreal.

For example, what is the precision of the following input values --

1.0
1.00
1.000
...
?

If I understood your original post the precisions would be 1, 2 and 3 respectively, but you'd not be able to discern that from the value getreal would return, which would be 1.0.

If you opt for a customized input routine that exploits getstring or grread you'll be able to determine how many characters are input after the decimal point, otherwise I think you're hooped.

On the other hand if you are simply trying to determine how many non zero values exist after the decimal point that's easy, though one has to be mindful of the limits of <computer> numerical precision.

<busy week/miss this place!>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge - Real number precision?
« Reply #6 on: November 02, 2006, 10:31:32 PM »
Give that man a cigar! Figuratively speaking.

Yes, I agree. My soultion involved getstring.
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Challenge - Real number precision?
« Reply #7 on: November 03, 2006, 08:51:22 AM »
Here is my solution ... take it for what it is ... no error checking ...

argument is a string

I looked at doing a test on real numbers, but the problems encountered required more programming than it was worth and only worked when the user was actually typing in the number and not passing it as an argument.

I have another unique solution that I will post later

Code: [Select]
(defun prec (val / fnd ndx repval)
  (setq ndx 0
repval (strlen val))
  (repeat repval
    (setq ndx (1+ ndx))
    (if (= (substr val ndx 1) ".")
      (setq val (substr val (1+ ndx) (strlen val))
    ndx 0 fnd T)
    )
  )
  (if fnd ndx 0)
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge - Real number precision?
« Reply #8 on: November 03, 2006, 09:06:04 AM »
Thanks Keith, looking forward to it.
And thanks to to Derwin.

Here is mine.
Code: [Select]
(defun prec (str / pos)
  (if (setq pos (vl-string-position 46 str))
    (- (strlen str) pos 1)
    0
  )
)
« Last Edit: November 03, 2006, 09:09:54 AM by CAB »
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Challenge - Real number precision?
« Reply #9 on: November 03, 2006, 09:15:25 AM »
I should have known there was a shorter solution .. but for whatever reason, I couldn't see it .. I am not awake yet ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Challenge - Real number precision?
« Reply #10 on: November 03, 2006, 09:27:16 AM »
Ok .. here is another way ...
much faster and shorter
Code: [Select]
(defun prec (str)
  (1-(strlen (vl-string-left-trim "-1234567890" str)))
)
« Last Edit: November 03, 2006, 09:28:59 AM by Keith™ »
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge - Real number precision?
« Reply #11 on: November 03, 2006, 09:30:00 AM »
I should have known there was a shorter solution .. but for whatever reason, I couldn't see it .. I am not awake yet ...
I spend half my life that way & only sleep 6 hour a night. :)

nice soultion! :-o
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge - Real number precision?
« Reply #12 on: November 03, 2006, 09:58:48 AM »
This is a excerpt from the routine I am working on.
I just set this up as a test & example to display the results.
Code: [Select]
;;  Get user entry of a number & return the number & the precision of the entry
(defun c:test (/ incstr incvalue getnum)
  (defun getnum (str / num typ)
    (cond ((setq num (distof str 2)) (setq typ 2))
          ((setq num (distof str 1)) (setq typ 1))
          ((setq num (distof str 5)) (setq typ 5))
          ((setq num (distof str 3)) (setq typ 3))
          ((setq num (distof str 4)) (setq typ 4))
          ((setq num (read str))
           ;;  (setq typ 6)  return nil for this type
          )
    )
    (if typ (list num typ))
  )

  ;;  get user input of a number
  (while
    (progn
      (setq incstr
             (vl-string-trim " \t\n" (getstring t "\nEnter a real number: ")))
      (cond
        ((= incstr "")
         (prompt "\n***  User quit.  ***")
         nil ; exit loop
        )
        ((not (setq incvalue (car (getnum incstr))))
         (prompt "\n***  Not a valid number, try again.  ***")
         t ; stay in loop
        )
        ;;  get precision of entry  10.005 = 3  [thanks Keith]
        ((setq prec (1- (strlen (vl-string-left-trim "+-1234567890" incstr))))
         nil
        )
      ) ; end cond stmt
    )
  )  ; end while

  (and incvalue
       (print (rtos incvalue 2 prec))
       (print prec)
  )
  (princ)
)
« Last Edit: November 03, 2006, 10:00:10 AM by CAB »
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: Challenge - Real number precision?
« Reply #13 on: November 03, 2006, 10:46:40 AM »
Here's a quick bash to add to your things to play with Alan --

Code: [Select]
(defun _GetNumber ( msg )
    (   (lambda ( input / result fixed )
            (vl-some
               '(lambda ( base ) (setq result (distof input base)))
               '(2 1 5 3 4)
            )
            (if result
                (if (vl-string-position 46 input)
                    result ;; assume result is a real
                    (if (eq result (setq fixed (fix result)))
                        fixed  ;; assume result is an integer
                        result ;; assume result is a real
                    )
                )
            )
        )
        (getstring (if (eq 'str (type msg)) msg))
    )
)

If you don't care for the lambda thing --

Code: [Select]
(defun _GetNumber ( msg / input result fixed )

    (setq input (getstring (if (eq 'str (type msg)) msg)))

    (vl-some
       '(lambda ( base ) (setq result (distof input base)))
       '(2 1 5 3 4)
    )

    (if result
        (if (vl-string-position 46 input)
            result ;; assume result is a real
            (if (eq result (setq fixed (fix result)))
                fixed  ;; assume result is an integer
                result ;; assume result is a real
            )
        )
    )
)

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge - Real number precision?
« Reply #14 on: November 03, 2006, 11:12:34 AM »
Oh, more play toys! 8-)

Thanks
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.

pmvliet

  • Guest
Re: Challenge - Real number precision?
« Reply #15 on: November 03, 2006, 05:21:26 PM »
You guys are amazing!!

Throw out an idea and get a few answers...
Each answer then seems to involve less and less code.

Just shows me that less is more in a lot of ways!

I need to start learning more of this stuff!!

Thanks for the insight

Pieter