Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Joel Gluth
On Tue, Oct 18, 2011 at 3:47 PM, Tassilo Horn tass...@member.fsf.org wrote:
 Scott Hickey jscotthic...@gmail.com writes:
 And usually, you should refrain from using floating points at all, no
 matter if BigDecimal or Double.

I thought BigDecimal with was not a floating point in the traditional
sense (ie., subject to all of the usual rounding horror, unless you
ask it to be)? That is, you can do decimal calculations exactly using
it.
-- 
[what were the skies like when you were young?]

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Ben Smith-Mannschott
On Thu, Oct 20, 2011 at 11:17, Joel Gluth joel.gl...@gmail.com wrote:
 On Tue, Oct 18, 2011 at 3:47 PM, Tassilo Horn tass...@member.fsf.org wrote:
 Scott Hickey jscotthic...@gmail.com writes:
 And usually, you should refrain from using floating points at all, no
 matter if BigDecimal or Double.

 I thought BigDecimal with was not a floating point in the traditional
 sense (ie., subject to all of the usual rounding horror, unless you
 ask it to be)? That is, you can do decimal calculations exactly using
 it.

The term floating point is used to distingiush from fixed-point
schemes, such as deciding that We're going to store money as a 64 bit
integer representing the number of 100ths of a cent. Effectively,
fixing the decimal point thus: $1000..

BigDecimal is an arbitrary precision integer and a scale factor (think
of the scale factor as multiplying or dividing by a power of ten.) So,
in that sense the (decimal) point floats.

Unlike floating point in the IEEE754 sense, BigDecimal is decimal,
not binary, (so e.g. 0.1 has a terminating representation) and
arbitrary precision, so you can avoid rounding effects unless you're
dividing where you might have to round to truncate a non-terminating
representation.

// Ben

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Tassilo Horn
Joel Gluth joel.gl...@gmail.com writes:

 And usually, you should refrain from using floating points at all, no
 matter if BigDecimal or Double.

 I thought BigDecimal with was not a floating point in the traditional
 sense (ie., subject to all of the usual rounding horror, unless you
 ask it to be)? That is, you can do decimal calculations exactly using
 it.

You are right.  What I've meant that you don't want to use double or
float because it's not precise, and you don't want to use BigDecimal
because it's slower and, even more important, it'll throw an exception
if a given number cannot be represented as decimal number.

user= (/ 1M 3)
ArithmeticException Non-terminating decimal expansion; no exact
representable decimal result.  java.math.BigDecimal.divide
(BigDecimal.java:1616)

So the general suggestion is to stick to integers and rational numbers
as long as possible.

Bye,
Tassilo

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Scott Hickey
Thank for the replies and I appreciate the suggestions, however they some of 
the rationale behind them doesn't match well my experience.

First, BigDecimal is plenty fast the large business systems I've worked on. 
Actually, it has been plenty fast for every large business system I've 
worked on. This includes a group insurance rate engine that generated 
hundreds of thousands of calculations in each web request.

Second, storing values with implied decimal points is a nightmare. In the 
insurance application for example, some of the rate tables have precision to 
three decimal places, others to five. Picking some arbitrary value imposes 
code complexity throughout the whole in app in a very nasty way. Of course, 
what happens when you need to change that value two years from now?

Unless there's a really good performance issue for a given application, I 
would never pick implied decimal representation over BigDecimals.

Finally, for the business applications I've worked with, I haven't had to 
worry about representing all rationals, just base 10 numbers. In my 
experience, using BigDecimal by default for any number with a decimal point 
has worked out very well for balancing the needs of accuracy, speed and code 
complexity.

It appears that the answer to the original question is no, there is no way 
to configure the reader to default numbers with a decimal point to be 
BigDecimal instead of Double.

Scott Hickey

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Stuart Sierra
Scott: no, there is no way to configure the reader to default numbers with 
a decimal point to be BigDecimal instead of Double

Correct. But you can modify the Reader: it's just Java code.

-S

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Stuart Halloway
 It appears that the answer to the original question is no, there is no way 
 to configure the reader to default numbers with a decimal point to be 
 BigDecimal instead of Double.
 
 Scott Hickey

Reading a double implies that somebody upstream of you was using doubles, which 
violates the guarantees you want from BigDecimals.

Why is the upstream provider using doubles?

Stu

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Ben Smith-Mannschott
On Thu, Oct 20, 2011 at 22:50, Stuart Halloway
stuart.hallo...@gmail.com wrote:
 It appears that the answer to the original question is no, there is no way 
 to configure the reader to default numbers with a decimal point to be 
 BigDecimal instead of Double.

 Scott Hickey

 Reading a double implies that somebody upstream of you was using doubles, 
 which violates the guarantees you want from BigDecimals.

 Why is the upstream provider using doubles?

I don't follow. The OP has text, which Clojure is reading as doubles.
This only implies that upstream (which need not have been written in
Clojure) is producing numbers matching #[-]?[1-9][0-9]*[.][0-9]*|0,
because LispReader interprets that as Double. Whatever internal
representation this text was produced form may or may not have been
(binary) floating point initially.

It doesn't seem reasonable to assume that the OP's 'business'
applications I've built over the last 25 years could have known that
Clojure would come along later and expect to find M on the end of
every decimal number.

// Ben

// Ben

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Stuart Halloway
 It appears that the answer to the original question is no, there is no way 
 to configure the reader to default numbers with a decimal point to be 
 BigDecimal instead of Double.
 
 Scott Hickey
 
 Reading a double implies that somebody upstream of you was using doubles, 
 which violates the guarantees you want from BigDecimals.
 
 Why is the upstream provider using doubles?
 
 I don't follow. The OP has text, which Clojure is reading as doubles.
 This only implies that upstream (which need not have been written in
 Clojure) is producing numbers matching #[-]?[1-9][0-9]*[.][0-9]*|0,
 because LispReader interprets that as Double. Whatever internal
 representation this text was produced form may or may not have been
 (binary) floating point initially.
 
 It doesn't seem reasonable to assume that the OP's 'business'
 applications I've built over the last 25 years could have known that
 Clojure would come along later and expect to find M on the end of
 every decimal number.
 
 // Ben

Hmm, highly apropos discussion since Rich's talk on simplicity posted today. 

The reader does one thing: read Clojure data. Sometimes you need something 
else: read data written in another format. You might accomplish that by (1) 
making the reader able to do two things, adding a flag to deal with a specific 
format, or (2) by using a different reader for that job.

(2) is hands-down the right answer. 

Stu

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-20 Thread Ben Smith-Mannschott
On Thu, Oct 20, 2011 at 23:16, Stuart Halloway
stuart.hallo...@gmail.com wrote:
 It appears that the answer to the original question is no, there is no 
 way to configure the reader to default numbers with a decimal point to be 
 BigDecimal instead of Double.

 Scott Hickey

 Reading a double implies that somebody upstream of you was using doubles, 
 which violates the guarantees you want from BigDecimals.

 Why is the upstream provider using doubles?

 I don't follow. The OP has text, which Clojure is reading as doubles.
 This only implies that upstream (which need not have been written in
 Clojure) is producing numbers matching #[-]?[1-9][0-9]*[.][0-9]*|0,
 because LispReader interprets that as Double. Whatever internal
 representation this text was produced form may or may not have been
 (binary) floating point initially.

 It doesn't seem reasonable to assume that the OP's 'business'
 applications I've built over the last 25 years could have known that
 Clojure would come along later and expect to find M on the end of
 every decimal number.

 // Ben

 Hmm, highly apropos discussion since Rich's talk on simplicity posted today.

 The reader does one thing: read Clojure data. Sometimes you need something 
 else: read data written in another format. You might accomplish that by (1) 
 making the reader able to do two things, adding a flag to deal with a 
 specific format, or (2) by using a different reader for that job.

 (2) is hands-down the right answer.

No argument there. The Clojure reader's job is to read Clojure. If
your have some data that happens to be syntactically compatible, fine,
but if not the Clojure reader  isn't the right tool.

One obvious approach would be a to use a regular expression to
identify the numbers in the text to be parsed and #(BigDecimal.
^String %) to do the actual conversion.

// Ben

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-18 Thread Scott Hickey
I'm looking to avoid qualifying every number that has a decimal point with 
an M. For the business applications I've built over the last 25 years 
(credit card processing, healthcare claims, loans,etc.), there's never been 
a situation where inexact numbers were appropriate. 

That's why I was hoping for a global default for reading decimal numbers as 
BigDecimal instead of IEEE floats or doubles.

Scott Hickey

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Is there a reader setting support BigDecimal by default?

2011-10-18 Thread Tassilo Horn
Scott Hickey jscotthic...@gmail.com writes:

Hi Scott,

 I'm looking to avoid qualifying every number that has a decimal point
 with an M. For the business applications I've built over the last 25
 years (credit card processing, healthcare claims, loans,etc.), there's
 never been a situation where inexact numbers were appropriate.

Nobody wants inexact numbers, but one has to trade speed with exactness.

And usually, you should refrain from using floating points at all, no
matter if BigDecimal or Double.  Clojure has rationals, so unless you
have to use java.lang.Math's functions or PI and E constants, you can
possibly do all calculations with rationals, and when you really need a
float, use (bigdec my-rational) to coerce it to a BigDecimal.

 That's why I was hoping for a global default for reading decimal
 numbers as BigDecimal instead of IEEE floats or doubles.

I've just checked, and that's hard-coded in clojure.lang.LispReader.

Bye,
Tassilo

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Is there a reader setting support BigDecimal by default?

2011-10-17 Thread Scott Hickey
In some versions of Scheme or Lisp, there is a flag that you can set so that 
the reader will create exact numbers by default (BigDecimal) instead of 
inexact doubles.

Is there a way to do this in Clojure?

Scott Hickey

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Is there a reader setting support BigDecimal by default?

2011-10-17 Thread Meikel Brandmeyer
Hi,

you are looking for 1.3M?

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Is there a reader setting support BigDecimal by default?

2011-10-17 Thread Scott Hickey
I've been using 1.2 but I would be OK with a solution in any version.

Scott Hickey

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Is there a reader setting support BigDecimal by default?

2011-10-17 Thread Meikel Brandmeyer
Hi,

I meant this:

Clojure 1.2.0
user= (type 1.0M)
java.math.BigDecimal

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en