aaryan Posted June 2, 2012 Posted June 2, 2012 Hi all, 1, (setq a 26.6) (setq b (fix a) c (fix (* (abs (- a b)) 10))) => returns [b]6[/b]. 2, (setq a 25.7) (setq b (fix a) c (fix (* (abs (- a b)) 10))) => returns [b]6[/b]. Why my second point doesn't return me 7 as answer. Am i doing something wrong.. Please Help. Quote
pBe Posted June 2, 2012 Posted June 2, 2012 Weird huh? (defun _roundTruncated (a) (setq b (fix a) c (* (abs (- a b)) 10) d (fix (+ c (/ c (abs c) 2.0)))) ) (_roundtruncated 26.6) 6 (_roundtruncated 25.7) 7 Quote
Stefan BMR Posted June 2, 2012 Posted June 2, 2012 You can not trust computers these days: (setq a 25.7) (rtos (* 10 (- a (fix a))) 2 16) "6.999999999999992" So maybe: (atoi (rtos (* 10 (- a (fix a))) 2 0)) 7 Quote
pBe Posted June 2, 2012 Posted June 2, 2012 You can not trust computers these days: (setq a 25.7) [i](rtos (* 10 (- a (fix a))) 2 16)[/i] [i]"6.999999999999992"[/i] So maybe: I believe it has something to do with OS platforms (16/32/64) The reason why the second code of the OP "fails" is because fix returns a truncated real value. the fix function by definition: Returns the conversion of a real number into the nearest smaller integer so (* 10 (- a (fix a))) 2 16) is in fact 6.999999999999992 "fix" to the nearest smallest integer 6 (atoi (rtos (* 10 (- a (fix a))) 2 0)) 7 I figured thats what the OP will eventually use for assigning the value to an attribute. Quote
BlackBox Posted June 2, 2012 Posted June 2, 2012 I believe it has something to do with OS platforms (16/32/64) If so, then my old 64bit-p routine may be of use: http://www.cadtutor.net/forum/showthread.php?65194-lsp-help-for-loading-amp-unloading-arx-file-from-specified-folder!&p=445347&viewfull=1#post445347 HTH Quote
irneb Posted June 2, 2012 Posted June 2, 2012 Sounds a lot like the issues in these 2 threads: http://www.cadtutor.net/forum/showthread.php?60816-round-up-number-in-autolisp http://www.cadtutor.net/forum/showthread.php?60050-Help-What-wrong-in-Fix-defun Quote
pBe Posted June 2, 2012 Posted June 2, 2012 Good show Irneb/Renderman Stange indeed (setq a 25.7) (setq b (fix a) c (fix (* (abs (- a b)) 100))) [b]69[/b] perhaps (setq b (fix (* (abs (- a (fix a))) 10.01))) 7 (setq b (fix (* (abs (- a (fix a))) 100.01))) 70 Okay, here's a challenge (_OneTensHundreds 120) --- "Hundreds" (_OneTensHundreds 12) ---- "Tens" (_OneTensHundreds 124.25) --- "Hundreds" Math only, no conversion to string or to make it more interesting (_OneTensHundreds (114.25) 100 10 4 0.20 0.05 Hang on, i think i saw something similar at the swamp. (regarding coin change) Quote
Lee Mac Posted June 2, 2012 Posted June 2, 2012 Okay, here's a challenge (_OneTensHundreds 120) --- "Hundreds" (_OneTensHundreds 12) ---- "Tens" (_OneTensHundreds 124.25) --- "Hundreds" Math only, no conversion to string (defun f ( n ) (cdr (assoc (fix (/ (log n) (log 10))) '((0 . "Units") (1 . "Tens") (2 . "Hundreds") (3 . "Thousands")))) ) _$ (f 120) "Hundreds" _$ (f 12) "Tens" Quote
pBe Posted June 2, 2012 Posted June 2, 2012 Whoa.. that was fast See updated post Lee. up there /|\ Quote
Lee Mac Posted June 2, 2012 Posted June 2, 2012 or to make it more interesting (_OneTensHundreds (114.25) 100 10 4 0.20 0.05 Quick hack: (defun f ( n / x ) (if (< 0 n) (progn (setq x (expt 10 (fix (/ (log n) (log 10)))) x (* (fix (/ n x)) x) ) (cons x (f (- n x))) ) ) ) _$ (f 123) (100 20 3) [ ONLY FOR INTEGERS ] Quote
pBe Posted June 2, 2012 Posted June 2, 2012 Quick hack: [ ONLY FOR INTEGERS ] Neat... I am yet to write my version.. (c'mon google, show me formula....) Quote
BlackBox Posted June 2, 2012 Posted June 2, 2012 Good show Irneb/Renderman Cheers, Pbe! Math only, no conversion to string Translation: "Hey Lee, I bet you can't s o l v e t h i s !?" (in a taunting voice, which is assured to get Lee's attention) ... Math only Quote
pBe Posted June 2, 2012 Posted June 2, 2012 Translation: "Hey Lee, I bet you can't s o l v e t h i s !?" (in a taunting voice, which is assured to get Lee's attention) ... Math only Geez, thats not my intention, but i have to admit it does seem like it. . Anyhoo. the thing is, with the numeruous functions available for string handling, the challenge would be far too easy for you the REST of us ( oops I did it again ) . and the solution can be as simple as (vl-string-position (ascii ".") (rtos n 2)) where's the fun in that? Hence the MATH only And guys, stop fooling around and start coding Quote
irneb Posted June 2, 2012 Posted June 2, 2012 Good show Irneb/Renderman Thanks! Okay, here's a challenge (_OneTensHundreds 120) --- "Hundreds" (_OneTensHundreds 12) ---- "Tens" (_OneTensHundreds 124.25) --- "Hundreds" Before your update I was thinking: http://caddons.svn.sourceforge.net/viewvc/caddons/Libraries/Math.LSP?revision=65&view=markup Functions: int->word (line 1031) and int->wordth (1121) But those only work on integers, not reals. Mainly due to the problem around this thread. But I think you saw those already from another tread. Anyhow, I'm thinking about a ~fix routine which adds a minimal fraction before executing normal fix. Might alleviate the problem methinks. Quote
Lee Mac Posted June 2, 2012 Posted June 2, 2012 ...or to make it more interesting (_OneTensHundreds (114.25) 100 10 4 0.20 0.05 To comply with the challenge criteria: (defun f ( n / ff ) (defun ff ( n x / y ) (if (not (equal n 0.0 1e-) (cons (setq y (* x (fix (+ (/ n x) 1e-))) (ff (- n y) (/ x 10.0)) ) ) ) (ff n (expt 10.0 (fix (/ (log n) (log 10))))) ) _$ (f 114.25) (100.0 10.0 4.0 0.2 0.05) None too elegant, but dealing with floating point errors is always cumbersome... Quote
pBe Posted June 3, 2012 Posted June 3, 2012 (edited) To comply with the challenge criteria: _$ (f 114.25) (100.0 10.0 4.0 0.2 0.05) None too elegant, but dealing with floating point errors is always cumbersome... You should see my code (defun _breakdown (num / _dec base a b lst) (defun _dec (l n h b) (if (or (< l n h) (= l n)) (float l) (_dec h n (+ h b) b)) ) [color=blue] [b] (setq base '(10000.00 1000.0 100.0 10.0 1.0 0.1 0.001 0.0001)) [/b][/color] (while (and (setq a (car base) base (cdr base)) (setq run (_dec 0 num a a))) (if (or (/= run 0) (= run a)) (setq lst (cons run lst) num (- num (car lst)))) ) (reverse lst) ) (_breakdown 114.25) (100.0 10.0 4.0 0.2 0.04); Also its limited by the base variable.. I may need to add a sub to generate a list depending on the value of n. Edited June 3, 2012 by pBe Quote
pBe Posted June 3, 2012 Posted June 3, 2012 Thanks!Before your update I was thinking: http://caddons.svn.sourceforge.net/viewvc/caddons/Libraries/Math.LSP?revision=65&view=markup whew , thats a handful. Nice linky Thanks!Anyhow, I'm thinking about a ~fix routine which adds a minimal fraction before executing normal fix. Might alleviate the problem methinks. (setq b (fix (* (abs (- a (fix a))) 10.01))) 7 (setq b (fix (* (abs (- a (fix a))) 100.01))) 70 Hence the challenge Irneb, but it evolves to something else though Quote
Recommended Posts
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.