lib: implements `Float::to_precision` in a non native way
authorJean Privat <jean@pryen.org>
Sat, 20 Jul 2013 01:51:40 +0000 (21:51 -0400)
committerJean Privat <jean@pryen.org>
Sat, 20 Jul 2013 11:28:18 +0000 (07:28 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/string.nit
tests/sav/extern_methods.sav
tests/sav/test_attr_easy.res
tests/sav/test_attr_easy.sav
tests/sav/test_ffi_c_simple.sav
tests/sav/test_float.res
tests/sav/test_float.sav [deleted file]
tests/sav/test_json.res
tests/sav/test_math.res [moved from tests/sav/test_math.sav with 58% similarity]
tests/sav/test_string_to_f.res
tests/sav/test_string_to_f.sav [deleted file]

index dc00916..0bc9e94 100644 (file)
@@ -783,7 +783,7 @@ end
 redef class Float
        # Pretty print self, print needed decimals up to a max of 6.
        redef fun to_s do
-               var str = to_precision( 6 )
+               var str = to_precision( 3 )
                var len = str.length
                for i in [0..len-1] do
                        var j = len-1-i
@@ -800,7 +800,30 @@ redef class Float
        end
 
        # `self' representation with `nb' digits after the '.'.
-       fun to_precision(nb: Int): String import String::from_cstring `{
+       fun to_precision(nb: Int): String
+       do
+               if nb == 0 then return self.to_i.to_s
+               var f = self
+               for i in [0..nb[ do f = f * 10.0
+               if self > 0.0 then
+                       f = f + 0.5
+               else
+                       f = f - 0.5
+               end
+               var i = f.to_i
+               if i == 0 then return "0.0"
+               var s = i.to_s
+               var sl = s.length
+               if sl > nb then
+                       var p1 = s.substring(0, s.length-nb)
+                       var p2 = s.substring(s.length-nb, nb)
+                       return p1 + "." + p2
+               else
+                       return "0." + ("0"*(nb-sl)) + s
+               end
+       end
+
+       fun to_precision_native(nb: Int): String import String::from_cstring `{
                int size;
                char *str;
 
index 3c62c68..edb48b0 100644 (file)
@@ -1,5 +1,5 @@
 144
 sleeping 1 second...
-0.463648
+0.464
 from C: self + fib(self) = 29
 from Nit: self + fib(self) = 29
index 83e00be..1b2af61 100644 (file)
@@ -5,7 +5,7 @@ test_attr_easy.nit:19,43--53: Warning: Expression is already a Float.
 test_attr_easy.nit:20,5--16: Warning: Expression is already a String.
 test_attr_easy.nit:20,20--26: Warning: Expression is already a B.
 test_attr_easy.nit:20,44--62: Warning: Expression is already a HashMap[Int, B].
-true false 12345 1.234499 asdf true
+true false 12345 1.234 asdf true
 an instance of B
 true true true true
 true true true true
index dce6637..7862a1e 100644 (file)
@@ -5,7 +5,7 @@ test_attr_easy.nit:19,43--53: Warning: Expression is already a Float.
 test_attr_easy.nit:20,5--16: Warning: Expression is already a String.
 test_attr_easy.nit:20,20--26: Warning: Expression is already a B.
 test_attr_easy.nit:20,44--62: Warning: Expression is already a HashMap[Int, B].
-true false 12345 1.2345 asdf true
+true false 12345 1.235 asdf true
 an instance of B
 true true true true
 true true true true
index f6af2b9..abe9027 100644 (file)
@@ -1,4 +1,4 @@
-5.12345
+5.123
 C implementation!
 in C!
 42
index 11ac227..cd932ee 100644 (file)
@@ -1,7 +1,7 @@
 5.500000
 0.550000
-12.345678
-12.345678
-12.345
+12.346
+12.346
+12.346
 12.3
 12
diff --git a/tests/sav/test_float.sav b/tests/sav/test_float.sav
deleted file mode 100644 (file)
index 2e037f7..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-5.500000
-0.550000
-12.345679
-12.345679
-12.346
-12.3
-12
index 16f85de..146e88b 100644 (file)
@@ -1,6 +1,6 @@
-{"int":"1234","float":"0.1234","str":"str","null": null}
+{"int":"1234","float":"0.123","str":"str","null": null}
 {"int":1234,"float":0.123400,"str":"str","null":null}
-{"arr":"123","obj":"{"int":"123","float":"-234.449997"}"}
+{"arr":"123","obj":"{"int":"123","float":"-234.45"}"}
 {"arr":[1,2,3],"obj":{"int":123,"float":-234.449997}}
-{"arr":"12.3str","obj":"{"int":"123","float":"-234.449997"}"}
+{"arr":"12.3str","obj":"{"int":"123","float":"-234.45"}"}
 {"arr":[1,2.300000,null,"str"],"obj":{"int":123,"float":-234.449997}}
similarity index 58%
rename from tests/sav/test_math.sav
rename to tests/sav/test_math.res
index 13e81d2..4ea9f3c 100644 (file)
@@ -1,10 +1,10 @@
-3.141593
+3.142
 -1.0
--0.0
 0.0
-0.785398
 0.0
-1.570796
+0.785
+0.0
+1.571
 5.0
 1.0
 0.0
index 0dcace2..f984e5d 100644 (file)
@@ -1,3 +1,3 @@
-0.123450
+0.123
 1234.0
-0.989897
+0.99
diff --git a/tests/sav/test_string_to_f.sav b/tests/sav/test_string_to_f.sav
deleted file mode 100644 (file)
index 46e980c..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-0.12345
-1234.0
-0.989898