Forcing divisions to be a machine-precision floating point divisions instead of multiplication by inverse

3

I want to imitate machine-precision, de facto IEEE division on Mathematica. On my quick trials I find this surprisingly hard to accomplish, since Mma rewrites a / b as a (b^-1) and these are not identical on finite-precision math.

For an explicit example, consider the following:

49. / 49. // FullForm

0.9999999999999999`

EDIT: As @J.M. points out, this works:

Divide[49., 49.] // FullForm

1.`

... but if I do the following, for instance, the finite-precision multiplication by inverse is seen again:

Divide[x, 49.] // FullForm

Times[0.02040816326530612`, x]

... which is bad.

How would I implement a "use native (IEEE) division semantics on machine-precision arguments of this division, even later down the evaluation chain" operation instead of Mma being too clever for its' own sake?

EDIT: In this specific case I'm not interested in other IEEE semantics, for instance strict order of operations (a + b + c is not the same as c + b + a!). I just want divisions to stay as divisions, which may be a bit of a half-way goal, but sounds more feasible to implement that the whole shebang.

kirma

Posted 2019-03-13T09:15:50.283

Reputation: 13 550

3Divide[49., 49.]? (I think this was discussed before.) – J. M.'s ennui – 2019-03-13T09:21:16.200

@J.M.isslightlypensive Hmmh... maybe I should rephrase my question somehow. It would seem that the "expected" IEEE behaviour is seen only in the case the computation can be completed with supplied arguments, otherwise it reverts to the a (b^-1) expansion very eagerly... – kirma – 2019-03-13T09:29:25.083

The previous question: https://mathematica.stackexchange.com/questions/161716

– Coolwater – 2019-03-13T11:53:45.140

Answers

3

You can define your own divide function:

divide[a_Real, b_Real] := Divide[a, b]
MakeBoxes[divide[a_, b_], StandardForm] := MakeBoxes[Divide[a, b], StandardForm]

Then:

divide[49., 49.]

1.

And:

r = divide[x, 49.]
r /. x->49.

x/49.

1.

Some more work is needed to define how divide works with other arithmetical operations.

Carl Woll

Posted 2019-03-13T09:15:50.283

Reputation: 112 778