# Decimal comparisons

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Thu Sep 18 19:47:51 PDT 2008

```Mark S. Miller wrote:
> * Like Crock, I prefer that typeof 1m === 'number'. However, unlike Crock's
> and Brendan's agreement, I think this should be unconditional. I think it
> would be horrible for 'typeof X' to depend not only on the value of X but
> also on the mode of the program unit in which the typeof appears. Please
> don't do that.

Strongly agreed. The existing context-dependent operations in the ES3 spec
(such as the hint to [[DefaultValue]]) are bad enough; we don't want to add
more of them.

> On Wed, Sep 17, 2008 at 12:49 PM, Brendan Eich wrote:
>> I do not believe anyone wants typeof 1.5m != typeof 1.1m.
>
> I agree that typeof 1.5m == typeof 1.1m. However, I do not agree that 1.1m
> == 1.1. These should be !=, since they represent distinct real numbers. I do
> agree that 1.5m == 1.5, but only because they represent the same real
> number, not because they look the same.

Agreed. In that case, we would have:

x relop y  <=>  toReal(x) relop_Real toReal(y)

[where 'toReal' maps either a binary or decimal value to the corresponding
real value].

Note that the implementation of this semantics requires a primitive
operation that compares a binary value with a decimal value; it's not
sufficient to just have primitive binary->decimal conversion and decimal
comparison operations.

> I agree with Sam and apparently Crock and everyone else that mixed mode
> should coerce to decimal. Or, at least, I have no objection to that
> position.

Mixed-mode should coerce to decimal except for ==, !=, <=, >=, since in
those cases the definition above (based on comparing real values) should
take precedence.

(For < and >, the definition based on comparing real values, and the
one based on coercion to decimal, are equivalent.)

> Were we to adopt this, then I think "use decimal" should condition only
> whether an unqualified numeric literal be interpreted as binary or decimal
> floating point. We should then have a suffix which means binary floating
> point, so you can say it explicitly. Nothing else about the numerics should
> be conditioned by the pragma.

I have no strong opinion on this. Note, however, that under this semantics
the value of Math.PI, for example, would have to stay as binary; it wouldn't
implicitly change to decimal in a "use decimal" program.

--
David-Sarah Hopwood

```