Created
March 3, 2018 16:18
-
-
Save varkor/8987a6ebd4404658476bcc4286af99b4 to your computer and use it in GitHub Desktop.
Mathematica functions for converting radians, in 32-bit floating point binary representation, to degrees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* Functions for converting between f32 and Real *) | |
f32ToReal[li_]:=SetPrecision[First[ImportString[ExportString[FromDigits[li,2],"UnsignedInteger32"],"Real32"]],Infinity]; | |
realToF32[x_]:=IntegerDigits[First[ImportString[ExportString[x,"Real32"],"UnsignedInteger32"]],2,32]; | |
(* Functions for converting between binary strings *) | |
binaryF32ToReal[x_]:=f32ToReal@ToExpression@StringSplit[x,""] | |
toDegrees[x_] := x/Degree | |
realToBinaryF32[x_] := StringJoin[ToString /@ realToF32[x]] | |
(* For example: *) | |
realToBinaryF32@toDegrees@binaryF32ToReal["00000000000001010001011110011111"] (* 4.67651e-40 *) | |
(* => "00000001000100011110000111111110" *) (* 2.6794435e-38 *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment