lib: fixed infinity on floats
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 5 Mar 2014 16:23:11 +0000 (11:23 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 5 Mar 2014 18:50:51 +0000 (13:50 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

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

index 340222a..8765a34 100644 (file)
@@ -48,6 +48,22 @@ redef class Float
        # Returns a random `Float` in `[0.0 .. self[`.
        fun rand: Float is extern "kernel_Float_Float_rand_0"
        fun hypot_with( b : Float ) : Float is extern "hypotf"
+
+       # Is the float an infinite value
+       # this function returns:
+       #
+       #  * 1 if self is positive infinity
+       #  * -1 if self is negative infinity
+       #  * 0 otherwise
+       fun is_inf: Int do
+               if is_inf_extern then
+                       if self < 0.0 then return -1
+                       return 1
+               end
+               return 0
+       end
+
+       private fun is_inf_extern: Bool is extern "isinf"
 end
 
 redef class Collection[ E ]
index 00bb14f..d4d03f7 100644 (file)
@@ -14,6 +14,7 @@
 # Basic manipulations of strings of characters
 module string
 
+import math
 intrude import collection # FIXME should be collection::array
 
 `{
@@ -1068,6 +1069,13 @@ redef class Float
        # `self` representation with `nb` digits after the '.'.
        fun to_precision(nb: Int): String
        do
+               var isinf = self.is_inf
+               if isinf == 1 then
+                       return "inf"
+               else if isinf == -1 then
+                       return  "-inf"
+               end
+
                if nb == 0 then return self.to_i.to_s
                var f = self
                for i in [0..nb[ do f = f * 10.0
diff --git a/tests/sav/test_float_inf.res b/tests/sav/test_float_inf.res
new file mode 100644 (file)
index 0000000..e1756d5
--- /dev/null
@@ -0,0 +1,4 @@
+1.0
+-1.0
+inf
+-inf
diff --git a/tests/test_float_inf.nit b/tests/test_float_inf.nit
new file mode 100644 (file)
index 0000000..b7e0696
--- /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 1.0 / 1.0
+print 1.0 / -1.0
+print 1.0 / 0.0
+print 1.0 / -0.0