abstract_text: Add the `to_sci` to get the string representation of a float in scient...
authorFlorian Deljarry <deljarry.florian@gmail.com>
Sun, 18 Aug 2019 20:17:01 +0000 (16:17 -0400)
committerFlorian Deljarry <deljarry.florian@gmail.com>
Thu, 22 Aug 2019 16:05:24 +0000 (12:05 -0400)
Signed-off-by: Florian Deljarry <deljarry.florian@gmail.com>

lib/core/text/abstract_text.nit

index 0072ec8..a4f80db 100644 (file)
@@ -2050,6 +2050,34 @@ redef class Float
                return str
        end
 
+       # Return the representation of `self`, with scientific notation
+       #
+       # Adpat the number of decimals as needed from 1 to a maximum of 6
+       # ~~~
+       # assert 12.34.to_se       == "1.234000e+01"
+       # assert 123.45.to_se.to_f.to_se  == "1.234500e+02"
+       # assert 0.001234.to_se  == "1.234000e-03"
+       # assert (inf).to_se == "inf"
+       # assert (nan).to_se == "nan"
+       # ~~~
+       fun to_sci: String
+       do
+               if is_nan then return "nan"
+
+               var isinf = self.is_inf
+               if isinf == 1 then
+                       return "inf"
+               else if isinf == -1 then
+                       return  "-inf"
+               end
+
+               var format = "%e".to_cstring
+               var size = to_precision_size_with_format(format)
+               var cstr = new CString(size + 1)
+               to_precision_fill_with_format(format, size + 1, cstr)
+               return cstr.to_s_unsafe(byte_length = size, copy = false)
+       end
+
        # `String` representation of `self` with the given number of `decimals`
        #
        # ~~~
@@ -2112,6 +2140,16 @@ redef class Float
        private fun to_precision_fill_hexa(size: Int, cstr: CString) `{
                snprintf(cstr, size, "%a", self);
        `}
+
+       # The lenght of `self` in the specific given c `format`
+       private fun to_precision_size_with_format(format: CString): Int`{
+               return snprintf(NULL, 0, format, self);
+       `}
+
+       # Fill `cstr` with `self` in the specific given c `format`
+       private fun to_precision_fill_with_format(format: CString, size: Int, cstr: CString) `{
+               snprintf(cstr, size, format, self);
+       `}
 end
 
 redef class Char