I was trying to convert some arbitrary-precision numbers to machine-precision numbers using N[myNumber,MachinePrecision]. But, although my test number did lose some precision, it still had a very strange exponent:

number = 5.803736411761291186334053015446685`16*^-400;
number1 = SetPrecision[number, MachinePrecision]


As $10^{-400}$ is not representable in IEEE 754 binary64, and I'm on x86 where this format is native, I tried checking what precision the number has, and it appears



This might be OK if truly machine-precision numbers gave the same, but they don't:



Finally, I've checked my number given by SetPrecision using FullForm:



So, apparently, SetPrecision won't give me machine number. How then do I convert my arbitrary-precision number to machine number? (I'd expect the $~10^{-400}$ value to be rounded down to machine zero.)

  • 2
    $\begingroup$ N[number]?? From Documentation Center page "MachinePrecision": "MachinePrecision is the symbol representing the number of decimal digits used by numerical functions such as N, NIntegrate, and NSum in the Wolfram Language for machine-precision computations." $\endgroup$ – murray Nov 30 '17 at 22:03
  • 1
    $\begingroup$ @murray have you actually tried it? Its result is not different from that from SetPrecision[number,MachinePrecision]. $\endgroup$ – Ruslan Dec 1 '17 at 6:02
  • $\begingroup$ Related: (37764) $\endgroup$ – Mr.Wizard Jul 24 '18 at 4:47

This situation is comparable to

$MinMachineNumber / 2

automatically giving an arbitrary precision result.

(* 15.9546 *)

Mathematica detects the underflow condition and switches to arbitrary precision arithmetic. This can be turned off:

SetSystemOptions["CatchMachineUnderflow" -> False]

Now the result of $MinMachineNumber/2 has MachinePrecision. The result is not 0., but a denormal number, I believe (my knowledge is lacking in this area). If you divide by a number larger than 10^$MachinePrecision then you get a 0..

(* 1.11254*10^-308 *)

(* 2.5*10^-323 *)

(* 0. *)

SetPrecision works as you want it, too.

number = 5.803736411761291186334053015446685`16*^-400;
number1 = SetPrecision[number, MachinePrecision]
(* 0. *)

SetPrecision will not create denormal numbers, it seems.

SetPrecision[1.2`16*^-308, MachinePrecision]
(* 0. *)
  • $\begingroup$ With V11.1.1 running on OS X 10.10.2, I do not get the OP's result from SetPrecision, not yours. $\endgroup$ – m_goldberg Dec 1 '17 at 6:06
  • $\begingroup$ Wow never supposed Mathematica would use allow denormals (they are performance killers for machine-precision computations). Is there any way to enable Flush-To-Zero mode in Mathematica (other than attaching a debugger and change MXCSR manually :) )? $\endgroup$ – Ruslan Dec 1 '17 at 6:14
  • $\begingroup$ @m_goldberg Sorry, I do not understand. What do you get precisely when underflow checking is enabled/disabled? $\endgroup$ – Szabolcs Dec 1 '17 at 7:13
  • $\begingroup$ I did not read your post carefully enough to realize the results you showed applied to the case where catching machine overflow had been turned off. $\endgroup$ – m_goldberg Dec 2 '17 at 0:18
number = 5.803736411761291186334053015446685`16*^-400;
number + 0.
(* 0. *)

N makes arbitrary precision numbers when either given a second argument or a number not representable by a machine number as its first argument. However, machine numbers in expressions generally coerce arithmetic into the machine domain.


Another way:

toMPReal = Compile[x, x][#] &(*/.c_Compile :> RuleCondition[c]*);

(*  0.  *)

Uncomment the replacement rule to pre-compile the function.


Starting in M11.3, SetPrecision will always produce a machine number when the second argument is MachinePrecision. The documentation for SetPrecision was unfortunately not updated with this change. Also, this means that the system option SetSystemOptions["CatchMachineUnderflow" -> True] no longer does anything. So, now you get the desired behavior without modifying any system options:

number = 5.803736411761291186334053015446685`16*^-400;
number1 = SetPrecision[number,MachinePrecision]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.