Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8fde9a696b17
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b85bb14ea703
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Dec 28, 2020

  1. HexLiteral

    eggrobin committed Dec 28, 2020
    Copy the full SHA
    40ffa40 View commit details

Commits on Dec 29, 2020

  1. Merge pull request #2828 from eggrobin/literals

    A HexLiteral for the IEEE754FloatingPoint package
    eggrobin authored Dec 29, 2020
    Copy the full SHA
    b85bb14 View commit details
Showing with 41 additions and 0 deletions.
  1. +41 −0 mathematica/floating_point.wl
41 changes: 41 additions & 0 deletions mathematica/floating_point.wl
Original file line number Diff line number Diff line change
@@ -35,6 +35,9 @@ UlpPlot;
Bits;


HexLiteral;


CorrectlyRound;
\[LeftAngleBracket]x_\[RightAngleBracket]:=CorrectlyRound[x];

@@ -87,6 +90,37 @@ Bits[n_]:=If[n>=0,"0","1"]<>
";"<>If[FractionalPart[Representation[Abs[n]]]==0,"",ToString/@RealDigits[N[FractionalPart[Representation[Abs[n]]],5],2,10,-1][[1]]<>"\[Ellipsis]"];


fullHexDigits:=Floor[(significandBits-1)/4]


leastFullHexDigitValue:=2^(significandBits-1)/16^fullHexDigits


leastHexDigitValue:=If[leastFullHexDigitValue>1,leastFullHexDigitValue/16,leastFullHexDigitValue]


HexLiteral[n_]:=If[n<0,"-",""]<>
"0x1."<>ToUpperCase[
IntegerString[
Mod[IntegerPart[Representation[Abs[n]]/leastFullHexDigitValue],16^fullHexDigits],16,fullHexDigits]<>
If[
leastHexDigitValue<1,
"'"<>IntegerString[Mod[IntegerPart[Representation[Abs[n]]/leastHexDigitValue],16],16,1],
""]<>
If[FractionalPart[Representation[Abs[n]]]==0,
"",
"'"<>ToString/@RealDigits[
N[FractionalPart[Representation[Abs[n]]/leastHexDigitValue],5],
16,3,-1][[1]]<>"\[Ellipsis]"]]<>
"p"<>ToString[IntegerPart[Representation[Abs[n]]/2^(significandBits-1)]-bias]<>
Switch[
{significandBits,exponentBits},
binary32,"f",
binary64,"",
x87extended,"l",
_,"_"<>ToString[significandBits]<>"_sigbits"]


smol=-12;
Log2OrSmol[x_]:=If[x==0,smol,If[Log2[x]<smol+3,smol+1,Log2[x]]];
UlpPlot[errors_,options___]:=Histogram[
@@ -159,3 +193,10 @@ EndPackage[]

(* ::Code:: *)
(*{Bits[Sqrt[2]],Bits[\[LeftAngleBracket]Sqrt[2]\[RightAngleBracket]],Bits[-Sqrt[2]],Bits[\[LeftAngleBracket]-Sqrt[2]\[RightAngleBracket]]}//Column*)


(* ::Code:: *)
(*Table[*)
(*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*)