1 # print out floats in decimal 2 # https://research.swtch.com/ftoa 3 # 4 # Basic idea: 5 # Ignoring sign, floating point numbers are represented as 1.mantissa * 2^exponent 6 # Therefore, to print a float in decimal, we need to: 7 # - compute its value without decimal point 8 # - convert to an array of decimal digits 9 # - print out the array while inserting the decimal point appropriately 10 # 11 # Basic complication: the computation generates numbers larger than an int can 12 # hold. We need a way to represent big ints. 13 # 14 # Key insight: use a representation for big ints that's close to what we need 15 # anyway, an array of decimal digits. 16 # 17 # Style note: we aren't creating a big int library here. The only operations 18 # we need are halving and doubling. Following the link above, it seems more 19 # comprehensible to keep these operations inlined so that we can track the 20 # position of the decimal point with dispatch. 21 # 22 # This approach turns out to be fast enough for most purposes. 23 # Optimizations, however, get wildly more complex. 24 25 fn test-write-float-decimal-approximate-normal { 26 var s-storage: (stream byte 0x10) 27 var s/ecx: (addr stream byte) <- address s-storage 28 # 0.5 29 var half/xmm0: float <- rational 1, 2 30 write-float-decimal-approximate s, half, 3 31 check-stream-equal s, "0.5", "F - test-write-float-decimal-approximate-normal 0.5" 32<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><title>Python: module ranger.ext.accumulator</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head><body bgcolor="#f0f0f8"> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> <tr bgcolor="#7799ee"> <td valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.ext.html"><font color="#ffffff">ext</font></a>.accumulator</strong></big></big></font></td ><td align=right valign=bottom ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/ext/accumulator.py">/home/hut/work/ranger/ranger/ext/accumulator.py</a></font></td></tr></table> <p></p> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ee77aa"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> <td width="100%"><dl> <dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a> </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="ranger.ext.accumulator.html#Accumulator">Accumulator</a> </font></dt></dl> </dd> </dl> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom> <br> <font color="#000000" face="helvetica, arial"><a name="Accumulator">class <strong>Accumulator</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr> <tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td> <td width="100%">Methods defined here:<br> <dl><dt><a name="Accumulator-__init__"><strong>__init__</strong></a>(self)</dt></dl> <dl><dt><a name="Accumulator-correct_pointer"><strong>correct_pointer</strong></a>(self)</dt></dl> <dl><dt><a name="Accumulator-get_list"><strong>get_list</strong></a>(self)</dt><dd><tt>OVERRIDE THIS</tt></dd></dl> <dl><dt><a name="Accumulator-move"><strong>move</strong></a>(self, relative<font color="#909090">=0</font>, absolute<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="Accumulator-move_to_obj"><strong>move_to_obj</strong></a>(self, arg, attr<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="Accumulator-pointer_is_synced"><strong>pointer_is_synced</strong></a>(self)</dt></dl> <dl><dt><a name="Accumulator-sync_index"><strong>sync_index</strong></a>(self, **kw)</dt></dl> <hr> Data descriptors defined here:<br> <dl><dt><strong>__dict__</strong></dt> <dd><tt>dictionary for instance variables (if defined)</tt></dd> </dl> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list of weak references to the object (if defined)</tt></dd> </dl> </td></tr></table></td></tr></table> </body></html>