lib: fixed nan on floats
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 5 Mar 2014 18:49:51 +0000 (13:49 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 5 Mar 2014 18:50:52 +0000 (13:50 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/standard/math.nit
lib/standard/string.nit
tests/sav/test_float_nan.res [new file with mode: 0644]
tests/test_float_nan.nit [new file with mode: 0644]

index 8765a34..7f7f0c4 100644 (file)
@@ -49,6 +49,8 @@ redef class Float
        fun rand: Float is extern "kernel_Float_Float_rand_0"
        fun hypot_with( b : Float ) : Float is extern "hypotf"
 
+       fun is_nan: Bool is extern "isnan"
+
        # Is the float an infinite value
        # this function returns:
        #
index f080e2a..e1505df 100644 (file)
@@ -1051,7 +1051,7 @@ redef class Float
        # Pretty print self, print needoed decimals up to a max of 3.
        redef fun to_s do
                var str = to_precision( 3 )
-               if is_inf != 0 then return str
+               if is_inf != 0 or is_nan then return str
                var len = str.length
                for i in [0..len-1] do
                        var j = len-1-i
@@ -1070,6 +1070,8 @@ redef class Float
        # `self` representation with `nb` digits after the '.'.
        fun to_precision(nb: Int): String
        do
+               if is_nan then return "nan"
+
                var isinf = self.is_inf
                if isinf == 1 then
                        return "inf"
diff --git a/tests/sav/test_float_nan.res b/tests/sav/test_float_nan.res
new file mode 100644 (file)
index 0000000..fd708f0
--- /dev/null
@@ -0,0 +1,4 @@
+0.0
+nan
+nan
+nan
diff --git a/tests/test_float_nan.nit b/tests/test_float_nan.nit
new file mode 100644 (file)
index 0000000..43ee88a
--- /dev/null
@@ -0,0 +1,20 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2004-2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+print 0.0 / 1.0
+print 0.0 / 0.0
+print 0.0 / -0.0
+print ((-1.0).sqrt)