Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion == is not transitive?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Patrick Houk  
View profile  
 More options Oct 5 2012, 10:42 am
From: Patrick Houk <path...@gmail.com>
Date: Fri, 5 Oct 2012 07:42:44 -0700 (PDT)
Local: Fri, Oct 5 2012 10:42 am
Subject: Re: == is not transitive?
I was bitten by this a year ago and posted here:
http://groups.google.com/group/clojure/browse_frm/thread/9091ad790fc9...

My workaround is to call BigDecimal#stripTrailingZeros before passing
it to code that might compare it to some other number.

user> (== 1 (.stripTrailingZeros 1.0M))
true

However, there is an edge case:

user> (== 0 (.stripTrailingZeros 0.0M))
false

Which is due to this Java bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6480539

So I use a function like the following instead of bigdec to parse
BigDecimals.

(defn parse-stripped-bigdec [^String value-str]
  ;; Note that bigdec uses reflection (at least in Clojure 1.2.1)
  (let [n (java.math.BigDecimal. value-str)]
    (if (zero? n) 0M (.stripTrailingZeros n))))

Be aware that stripping the zeros changes how the BigDecimal is
formatted by #toString.

user> (str (parse-stripped-bigdec "10"))
"1E+1"

So, I have a special case for BigDecimal that calls #toPlainString in
the places where I do not want exponential format.

user> (.toPlainString (parse-stripped-bigdec "10"))
"10"

I hope that helps.
- Pat

On Oct 4, 8:39 pm, Ben Wolfson <wolf...@gmail.com> wrote:

> user> [(== 0 0.0) (== 0.0 0.0M) (== 0.0M 0)]
> [true true false]
> user> [(== 0 0.0 0.0M) (== 0 0.0M 0.0) (== 0.0 0 0.0M) (== 0.0 0.0M 0)
> (== 0.0M 0.0 0) (== 0.0M 0 0.0)]
> [true false false false true false]

> --
> Ben Wolfson
> "Human kind has used its intelligence to vary the flavour of drinks,
> which may be sweet, aromatic, fermented or spirit-based. ... Family
> and social life also offer numerous other occasions to consume drinks
> for pleasure." [Larousse, "Drink" entry]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.