Author Topic: Tiny topic  (Read 2150 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Tiny topic
« on: November 23, 2005, 10:54:17 AM »
Do you ever have need to normalize values that are approaching, but not quite zero, like -1.83691e-016, that is, have it evaluated as zero if it's smaller than an arbitrary threshold?

I had a colleague who needed something like this so I thought I'd share it.

(defun Normalize ( x )
    (cond
        ((zerop x) x)
        ((eq 'int (type x)) x)
        ((equal 0 x 1e-15) 0.0)
        (x)
    )
)
   

(Normalize 0)

==> 0

(Normalize 0.0)

==> 0.0

(Normalize 1e-5)

==> 1.0e-005

(Normalize -1.83691e-016)

==> 0.0

(mapcar 'Normalize '(1.0 -1.83691e-016 0.0))

==> (1.0 0.0 0.0)
[/color]


The idea is that it won't change ints to reals and vice versa, but it does return 0.0 for minuscule values.

Obviously the 1e-15 fuzz factor is subjective.

What would you use?

Any other thoughts?

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: Tiny topic
« Reply #1 on: November 23, 2005, 11:16:22 AM »
Interesting, I have always used just the fuzz.

Would you ever have need of this?
Code: [Select]
((equal 0 (- x (fix x)) 1e-15) (float (fix x)))
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: Tiny topic
« Reply #2 on: November 23, 2005, 11:43:27 AM »
Interesting, I have always used just the fuzz.

Would you ever have need of this?
Code: [Select]
((equal 0 (- x (fix x)) 1e-15) (float (fix x)))

Hmmm. let me play with that Allan. Thank you.

Tangent: My normalize function was lifted from this little thing I penned long ago:

(defun IsZero ( x )
    (or (zerop x)
        (equal x 0 1e-15)
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tiny topic
« Reply #3 on: November 23, 2005, 12:00:05 PM »
Interesting func Allan: Round a number to its integer value if it's fractional portion is below the threshold.

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