ES4 decimal

Sam Ruby rubys at intertwingly.net
Sun Jul 27 19:49:24 PDT 2008


In addition to the "BigDecimal" like interface that is the basis for 
ES3.1 support for decimal (and possibly the lower level "static" 
interface that Allen asked me to consider), the intent is that ES4 also 
has a number of "affordances" that make a number of common operations 
easier.  These take the form of decimal literal syntax and both binary 
and unary operators.  Note: these conveniences can never take the place 
of the named methods as they have no mechanism for accepting a context 
argument.

To aid in exploration of the implications of these operators, I've 
implemented a large portion of this changes on top of SpiderMonkey:

   http://code.intertwingly.net/public/hg/js-decimal/

Proposed spec text will follow, but below is a summary in case it sparks 
any discussion.

Syntax for literals are the same as for doubles, but with a 'm' suffix. 
  It is just the semantics that differ.  At the present time, there is 
no explicit syntax for NaNs or Infinitys.

When a Decimal is found in a context where a Boolean is expected, 0m (of 
any precision, e.g. 0.00m) is treated as false, all other values are 
treated as true.

Unary plus and minus on Decimal values produce Decimal values.

When both arguments to +, -, *, /, and % operators are Decimal, the 
result is a Decimal.  Otherwise, the behavior is as any Decimal values 
are converted to a string.  A few examples:

js> 1.21  - 1.11
0.09999999999999987
js> 1.21  - 1.11m
0.09999999999999987
js> 1.21m - 1.11
0.09999999999999987
js> 1.21m - 1.11m
0.10

Similar behavior is defined for relational operators.  == compares for 
equality independent of scale (i.e., 0.1m == 0.10m == 0.1).  If either 
argument to === is a Decimal, the result is only true both arguments are 
of type decimal, and have the same value and scale.

Not implemented at this time are 'bitwise' operators including  |, ^, &, 
<<, and >>.

That's pretty much it.

If people would like me to build binaries for a specific operating 
system, let me know and I will see what I can do.

- Sam Ruby




More information about the Es4-discuss mailing list