Skip to content

Commit a115a4a

Browse files
committed
Value: Add support for negative infinity
1 parent 44d5d03 commit a115a4a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/scratch/value.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Value::Value(SpecialValue specialValue) :
6969
{
7070
if (specialValue == SpecialValue::Infinity)
7171
m_isInfinity = true;
72+
else if (specialValue == SpecialValue::NegativeInfinity)
73+
m_isNegativeInfinity = true;
7274
else if (specialValue == SpecialValue::NaN)
7375
m_isNaN = true;
7476
else {
@@ -83,12 +85,18 @@ Value::Type Value::type() const
8385
return m_type;
8486
}
8587

86-
/*! Returns true if the value is not finite. */
88+
/*! Returns true if the value is infinity. */
8789
bool Value::isInfinity() const
8890
{
8991
return m_isInfinity;
9092
}
9193

94+
/*! Returns true if the value is negative infinity. */
95+
bool Value::isNegativeInfinity() const
96+
{
97+
return m_isNegativeInfinity;
98+
}
99+
92100
/*! Returns true if the value is NaN (Not a Number). */
93101
bool Value::isNaN() const
94102
{
@@ -126,6 +134,8 @@ float Value::toNumber() const
126134
case Type::Special:
127135
if (m_isInfinity)
128136
return std::numeric_limits<float>::infinity();
137+
else if (m_isNegativeInfinity)
138+
return -std::numeric_limits<float>::infinity();
129139
else
130140
return 0;
131141
}
@@ -169,6 +179,8 @@ std::string Value::toString() const
169179
case Type::Special:
170180
if (m_isInfinity)
171181
return "Infinity";
182+
else if (m_isNegativeInfinity)
183+
return "-Infinity";
172184
else
173185
return "NaN";
174186
}
@@ -199,6 +211,21 @@ float Value::stringToFloat(std::string s, bool *ok)
199211
{
200212
if (ok)
201213
*ok = false;
214+
215+
if (s == "Infinity") {
216+
if (ok)
217+
*ok = true;
218+
return std::numeric_limits<float>::infinity();
219+
} else if (s == "-Infinity") {
220+
if (ok)
221+
*ok = true;
222+
return -std::numeric_limits<float>::infinity();
223+
} else if (s == "NaN") {
224+
if (ok)
225+
*ok = true;
226+
return 0;
227+
}
228+
202229
std::string digits = "0123456789.eE+-";
203230
for (char c : s) {
204231
if (digits.find(c) == std::string::npos) {

src/scratch/value.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class LIBSCRATCHCPP_EXPORT Value
1515
enum class SpecialValue
1616
{
1717
Infinity,
18+
NegativeInfinity,
1819
NaN
1920
};
2021

@@ -38,6 +39,7 @@ class LIBSCRATCHCPP_EXPORT Value
3839
Type type() const;
3940

4041
bool isInfinity() const;
42+
bool isNegativeInfinity() const;
4143
bool isNaN() const;
4244
bool isNumber() const;
4345
bool isBool() const;
@@ -54,6 +56,7 @@ class LIBSCRATCHCPP_EXPORT Value
5456
static float stringToFloat(std::string s, bool *ok = nullptr);
5557

5658
bool m_isInfinity = false;
59+
bool m_isNegativeInfinity = false;
5760
bool m_isNaN = false;
5861
Type m_type;
5962
float m_numberValue = 0;

0 commit comments

Comments
 (0)