Last active
July 11, 2017 00:43
-
-
Save kybr/3577b4e69493ec7fda1b10101d6b4774 to your computer and use it in GitHub Desktop.
Is this a bad idea?
This file contains 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
#!/usr/bin/env perl6 | |
use v6.c; | |
# see https://en.wikipedia.org/wiki/Single-precision_floating-point_format | |
sub float(Buf $b) returns Rat { | |
my Int $i = $b[3]+<24 +| $b[2]+<16 +| $b[1]+<8 +| $b[0]; | |
my Int $sign-bit = ($i +& 0b10000000_00000000_00000000_00000000) +> 31; | |
my Int $exponent = ($i +& 0b01111111_10000000_00000000_00000000) +> 23; | |
my Int $fraction = ($i +& 0b00000000_01111111_11111111_11111111); | |
my $thing = ((-1)**$sign-bit) * (1 + $fraction / (2**23)) * (2**($exponent - 127)); | |
# sometimes $thing is a small Num, so F that. uncomment this line | |
# to throw away small Nums... **Is this a bad idea?** | |
# $thing ~~ Num ?? 0/1 !! $thing; | |
} | |
# test float sub with PI | |
say float(Buf.new(0xdb, 0x0f, 0x49, 0x40)); | |
# try a bunch of random buffers. | |
for ^100 { | |
try { | |
CATCH { default { .Str.say } } | |
say float(Buf.new((^256).pick, (^256).pick, (^256).pick, (^256).pick)); | |
} | |
} | |
# output: | |
# | |
# 3.14159274 | |
# -184815766020774952960 | |
# Type check failed for return value; expected Rat but got Num (-5.44634129302561e-34) | |
# Type check failed for return value; expected Rat but got Num (1.46232472745395e-13) | |
# Type check failed for return value; expected Rat but got Num (-2.59997804933534e-36) | |
# -933729365187685911625728 | |
# -10339434640959341886177280 | |
# -1349181761705700491264 | |
# -0.00000000030238012 | |
# 12521785258036374543929786236928 | |
# Type check failed for return value; expected Rat but got Num (-7.75945548337306e-16) | |
# Type check failed for return value; expected Rat but got Num (-2.20862333277913e-14) | |
# 887489426358272 | |
# 0.1889915 | |
# -35706971677229127895278487436299075584 | |
# -1235545504330660185422954496 | |
# Type check failed for return value; expected Rat but got Num (-8.4933029835363e-26) | |
# -431510666608640 | |
# 0.00000000682154155 | |
# Type check failed for return value; expected Rat but got Num (-2.56890463303824e-24) | |
# 0.0000001030326828 | |
# Type check failed for return value; expected Rat but got Num (-1.88907596061435e-15) | |
# -284.539490 | |
# -9662638846588794691852434952683520 | |
# Type check failed for return value; expected Rat but got Num (9.88364650050003e-34) | |
# 5377074204464119808 | |
# Type check failed for return value; expected Rat but got Num (-1.2182033776642e-14) | |
# -1614144809730048 | |
# 229784856221850875831418617856 | |
# 0.00000001324852850 | |
# 16635182080 | |
# Type check failed for return value; expected Rat but got Num (-2.17191308726178e-28) | |
# Type check failed for return value; expected Rat but got Num (-1.82331140345028e-31) | |
# 42559149703168 | |
# -442624256320854818816 | |
# Type check failed for return value; expected Rat but got Num (-1.80937747127406e-25) | |
# 0.00000784685017 | |
# Type check failed for return value; expected Rat but got Num (-3.3224701304099e-32) | |
# 439863088216027407400275279872 | |
# 1488508362144585880908595200 | |
# Type check failed for return value; expected Rat but got Num (-2.03143717452926e-33) | |
# -0.094923958 | |
# Type check failed for return value; expected Rat but got Num (-1.26969710244861e-13) | |
# -26259353986288608178037850112 | |
# 20.6231060 | |
# 29626554384384 | |
# 0.0000001014863429 | |
# 76743579114265648287616860160 | |
# 4455084544 | |
# 52633650372770263520577060864 | |
# Type check failed for return value; expected Rat but got Num (-4.78444919130256e-16) | |
# -0.00000000028837843 | |
# -28309182464 | |
# 12101708923162502812932058906624 | |
# 76425184213063685870130500076400803840 | |
# -648120948710232251432960 | |
# 543766694886666928128 | |
# 291847445444768579547557548732228567040 | |
# -123285856 | |
# 6704497152 | |
# -48275100354528411648 | |
# 0.58809268 | |
# -594850621685760 | |
# 0.000000000216214394 | |
# -38574330466376220762363101135765504 | |
# 0.00000104667049 | |
# -0.00000001106536285 | |
# Type check failed for return value; expected Rat but got Num (5.08949052139638e-18) | |
# -188517938686580776262934760259584 | |
# 60675220968772206592 | |
# 92.204483 | |
# Type check failed for return value; expected Rat but got Num (-6.87988157712543e-28) | |
# Type check failed for return value; expected Rat but got Num (2.5286780807538e-33) | |
# Type check failed for return value; expected Rat but got Num (-2.70422945604408e-30) | |
# 0.000120265031 | |
# Type check failed for return value; expected Rat but got Num (1.61772179150216e-23) | |
# -443779922206484594688 | |
# 2813856644826510274022394218156130304 | |
# -168242988892312681054208 | |
# 910220255693897728 | |
# Type check failed for return value; expected Rat but got Num (4.82236402929119e-26) | |
# -0.00000000000427319681 | |
# -0.32469904 | |
# Type check failed for return value; expected Rat but got Num (6.11057825318675e-34) | |
# Type check failed for return value; expected Rat but got Num (-6.28441166259633e-16) | |
# -0.232138842 | |
# Type check failed for return value; expected Rat but got Num (-8.01459013877391e-26) | |
# -486818431307962384384 | |
# 570199048192 | |
# 20707272075845396471847640367104 | |
# 75423542395948581200792746196992 | |
# 11389.183594 | |
# -1781205537993126999035108380377088 | |
# Type check failed for return value; expected Rat but got Num (2.40374784617417e-23) | |
# -984927943076296582496256 | |
# 318007028665866883669355423929991168 | |
# Type check failed for return value; expected Rat but got Num (-3.95462028836968e-29) | |
# 5.79221010 | |
# Type check failed for return value; expected Rat but got Num (-1.02397990837404e-18) | |
# 16074211805024772534292185088 | |
# -0.000000000334138411 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment