#
# see `to_precision` for a custom precision.
redef fun to_s do
- var str = to_precision( 3 )
+ var str = to_precision(3)
if is_inf != 0 or is_nan then return str
var len = str.length
for i in [0..len-1] do
end
var size = to_precision_size(decimals)
- var cstr = new CString(size+1)
- to_precision_fill(decimals, size+1, cstr)
- return cstr.to_s_unsafe(byte_length=size, copy=false)
+ var cstr = new CString(size + 1)
+ to_precision_fill(decimals, size + 1, cstr)
+ return cstr.to_s_unsafe(byte_length = size, copy = false)
+ end
+
+ # Returns the hexadecimal (`String`) representation of `self` in exponential notation
+ #
+ # ~~~
+ # assert 12.345.to_hexa_exponential_notation == "0x1.8b0a3d70a3d71p+3"
+ # assert 12.345.to_hexa_exponential_notation.to_f == 12.345
+ # ~~~
+ fun to_hexa_exponential_notation: String
+ do
+ var size = to_precision_size_hexa
+ var cstr = new CString(size + 1)
+ to_precision_fill_hexa(size + 1, cstr)
+ return cstr.to_s_unsafe(byte_length = size, copy = false)
end
# Required string length to hold `self` with `nb` decimals
private fun to_precision_fill(nb, size: Int, cstr: CString) `{
snprintf(cstr, size, "%.*f", (int)nb, self);
`}
+
+ # The lenght of `self` in exponential hexadecimal notation
+ private fun to_precision_size_hexa: Int`{
+ return snprintf(NULL, 0, "%a", self);
+ `}
+
+ # Fill `cstr` with `self` in exponential hexadecimal notation
+ private fun to_precision_fill_hexa(size: Int, cstr: CString) `{
+ snprintf(cstr, size, "%a", self);
+ `}
end
redef class Char
# Generate a float value
#
# FIXME pass a Float, not a string
- fun float_instance(value: String): RuntimeVariable
+ fun float_instance(value: Float): RuntimeVariable
do
var t = mmodule.float_type
- var res = new RuntimeVariable("{value}", t, t)
+ var res = new RuntimeVariable("{value.to_hexa_exponential_notation}", t, t)
return res
end
end
redef class AFloatExpr
- redef fun expr(v) do return v.float_instance("{self.n_float.text}") # FIXME use value, not n_float
+ redef fun expr(v) do return v.float_instance(self.value.as(Float))
end
redef class ACharExpr