I would like to know if there is any way to convert decimal numbers to floating point, using the IEEE-754 standard, specifying whether it is simple or double. And vice versa, floating point to decimal specifying whether it is double or single.


1 Answer 1


Yes. You should be able to import and export singles, doubles, or quad-precision numbers in the IEEE standard. See documentation here.

Let's check. We start with some nice 140 digit numbers.

(someNumbers = RandomReal[{1, 200}, 4, WorkingPrecision -> 140])


{131.57843910491117542009357067044305298919523687461972590508545252797246656713683959501545442109954305145360196695641268407818906794518467693, 36.777212169625076229757244904535982444480287433912465096550209184759013144280916569005201142526738998958909685743061158332762603357972081895, 32.211986718696311559869777490390932918154744943543676822285668781237395283963457422161288449125141442835469928797914730615636279609237747203, 91.455311969662025855984807531478321762889779722600757410998496792433355267117887298097053278192108027867375906636235367744938744286139195293}

Let's export and import them as floats, doubles, and quads:

Export["/tmp/quads.dat", someNumbers, "Real128"];
Export["/tmp/doubles.dat", someNumbers, "Real64"];
Export["/tmp/floats.dat", someNumbers, "Real32"];
(floats = Import["/tmp/floats.dat", "Real32"]);
(doubles = Import["/tmp/doubles.dat", "Real64"]);
(quads = Import["/tmp/quads.dat", "Real128"]);

Now look at the accuracy relative to the original 140 digit Reals:

someNumbers - SetPrecision[floats, 140] // N


{-6.32966*10^-6, -1.88067*10^-6, 1.76948*10^-7, -2.66657*10^-6}

someNumbers - SetPrecision[doubles, 140] // N


{-7.80161*10^-15, 2.67301*10^-15, 2.41998*10^-15, 5.82303*10^-15}


someNumbers - SetPrecision[quads, 140] // N


{2.27275*10^-32, 4.51408*10^-33, 3.39809*10^-33, 1.31565*10^-33}

Let's convert within Mathematica going from doubles to floats:

floatsFromDoubles = ImportString[ExportString[doubles, "Real32"], "Real32"];

Do we land on the same thing as converting our 140 digit reals?

Yes: floatsFromDoubles===floats $\mapsto$ True.


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.