Jump to content

Recommended Posts

Posted

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.

Posted

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

Posted

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

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

Posted

Thanks a LOT pBe, Tharwat, Stefan BMR...

Posted

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)

Posted
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"

Posted

Whoa.. that was fast :thumbsup:

 

See updated post Lee. up there /|\

Posted
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 ]

Posted
Quick hack:

 

[ ONLY FOR INTEGERS ]

 

Neat... I am yet to write my version.. (c'mon google, show me formula....) :lol:

Posted
Good show Irneb/Renderman

 

Cheers, Pbe! :beer:

 

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 :rofl:

 

 

:P

Posted

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 :rofl::P

 

:lol: Geez, thats not my intention, but i have to admit it does seem like it. :rofl:.

 

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 :rofl:) .

 

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 :P

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

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

Posted (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 :lol:

 

(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 by pBe
Posted

 

whew :sweat:, thats a handful. Nice linky :thumbsup:

 

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 :)

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