Jump to content

Limit real variable between 2 numbers


phuynh

Recommended Posts

Need help to set "rScale" variable between 0 and 1

How can I limit user to enter real number that greater 0 but less than or equal 1 ?

 

Quote

(setq rScale (getreal (strcat "\nEnter scale ratio: ")))

 

Thanks!

 

Edited by phuynh
Link to comment
Share on other sites

Here's another... Not tested as it's typed away from CAD.

 

(while

    (null (< 0.0 (setq rScale (getreal "\nSpecify value: ")) 1.0))

    (princ "\nValue is not between 0 and 1")

    )

  • Like 1
Link to comment
Share on other sites

I always like to give the user a way to exit without forcing an error with Esc, e.g.:

(while
    (and
        (setq x (getreal "\nSpecify a number between 0 and 1: "))
        (not (<= 0.0 x 1.0))
    )
    (princ "\nNumber must be between 0 and 1.")
)
(if x
    ;; do stuff
)

 

  • Like 1
Link to comment
Share on other sites

Lee Mac,

 

I am big fan of you! and I learn lot from you and community.

 

I work on lisp code that set up axonometry (dimetric) view point in 3d space.

 

it is work but it sloppy, can you help?

 

 

Link to comment
Share on other sites

Try this

;;Get a real number x lying the closed interval [a b]
;; (_getreal 0 1 0.5 "\nSpecify a number between 0 and 1 <0.5>: ")
;; (_getreal 0 1 nil "\nSpecify a number between 0 and 1: ")
(defun _getreal (a b default mesg / x)
;;  (if (not default) (initget 1)) ;;Optional
 (setq x (vl-catch-all-apply (function getreal) (list mesg)))
 (cond
  ( (and default (not (numberp x))) default) ;;<Enter>, <Space>, <Esc>
  ( (and default (numberp x) (or (< x a) (> x b))) (_getreal a b default mesg))
  ( (and
     (not default)
     (or (not (numberp x)) (and (numberp x) (or (< x a) (> x b))))
    )
    (_getreal a b nil mesg)
  )
  (T x)
 )
) ;;_getreal

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...