Skip to content

Commit

Permalink
Merge pull request #4023 from pleroy/FP
Browse files Browse the repository at this point in the history
Add option RoundingMode to CorrectlyRound
pleroy authored Jun 8, 2024
2 parents d903a4f + 3116a5c commit 59e4f4b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion mathematica/floating_point.wl
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ NearestTiesToEven;
Toward0;
TowardPositiveInfinity;
TowardNegativeInfinity;
RoundingMode;


Representation;
@@ -84,10 +85,18 @@ e=IntegerPart[magnitude/2^(significandBits-1)];
If[e==0,
\[Mu]/2^(significandBits-1) 2^(1-bias),
sign(1+\[Mu]/2^(significandBits-1))2^(e-bias)]];
CorrectlyRound[x_]:=If[x==\[Infinity]||x==-\[Infinity],x,If[Abs[#]>=2^(bias+1),Sign[x]\[Infinity],#]&@FromRepresentation[correctlyRoundRepresentation[Representation[x]]]];
UlpDistance[x_,y_]:=Abs[Representation[x]-Representation[y]]


CorrectlyRound[x_,OptionsPattern[]]:=With[
{rounding=If[OptionValue[RoundingMode]===Automatic,correctlyRoundRepresentation,OptionValue[RoundingMode]]},
If[x==\[Infinity]||x==-\[Infinity],x,
If[Abs[#]>=2^(bias+1),Sign[x]\[Infinity],#]&
@FromRepresentation[rounding[Representation[x]]]]];
UlpDistance[x_,y_]:=Abs[Representation[x]-Representation[y]];
Options[CorrectlyRound]={RoundingMode->Automatic};


Bits[n_, extraBits_: 10]:=If[n>=0,"0","1"]<>
"|"<>IntegerString[IntegerPart[Representation[Abs[n]]/2^(significandBits-1)],2,exponentBits]<>
"|"<>IntegerString[Mod[IntegerPart[Representation[Abs[n]]],2^(significandBits-1)],2,significandBits-1]<>
@@ -208,3 +217,11 @@ EndPackage[]
(*SetFloatingPointFormat[format];*)
(*{#,{Bits[#],HexLiteral[#]},{Bits[CorrectlyRound[#]],HexLiteral[CorrectlyRound[#]]}}&/@{\[Pi],1/3,Sqrt[2],1+2^-format[[1]],1+2^(-format[[1]]-5)}//TableForm,*)
(*{format,{binary16,binary32,binary64,x87extended}}]//TableForm*)
(*SetRoundingMode[TowardPositiveInfinity];*)
(*CorrectlyRound[1/3]*)
(*SetRoundingMode[TowardNegativeInfinity];*)
(*CorrectlyRound[1/3]*)
(*CorrectlyRound[1/3,RoundingMode->Toward0]*)
(*CorrectlyRound[1/3,RoundingMode->TowardPositiveInfinity]*)
(*CorrectlyRound[1/3,RoundingMode->TowardNegativeInfinity]*)
(*CorrectlyRound[1/3,RoundingMode->NearestTiesToEven]*)

0 comments on commit 59e4f4b

Please sign in to comment.