From 8035efb52fa7201b2cf9c1fa1af093abbd09ced7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 6 Apr 2017 11:11:34 +0200 Subject: [PATCH] [poincare] When an integer is too big to be approximate by a float, return INFINITY Change-Id: I6be4847b6ac5ebd8974005b1ab16625670301562 --- poincare/src/integer.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 287167798..3ab775621 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -284,6 +284,12 @@ float Integer::privateApproximate(Context& context, AngleUnit angleUnit) const { bool sign = m_negative; uint8_t exponent = 126; + /* if the exponent is bigger then 255, it cannot be stored as a uint8. Also, + * the integer whose 2-exponent is bigger than 255 cannot be stored as a + * float (IEEE 754 floating point). The approximation is thus INFINITY. */ + if ((int)exponent + (m_numberOfDigits-1)*32 +numberOfBitsInLastDigit> 255) { + return INFINITY; + } exponent += (m_numberOfDigits-1)*32; exponent += numberOfBitsInLastDigit; @@ -308,6 +314,12 @@ float Integer::privateApproximate(Context& context, AngleUnit angleUnit) const { uint_result |= (exponent << 23); uint_result |= (mantissa >> (32-23-1) & 0x7FFFFF); + /* If exponent is 255 and the float is undefined, we have exceed IEEE 754 + * representable float. */ + if (exponent == 255 && isnan(float_result)) { + return INFINITY; + } + return float_result; }