Skip to content

Commit 18bc84a

Browse files
author
Lauren McCarthy
committed
adding handling for infinity to int, float, and hex, closes #3658
1 parent cc25ab5 commit 18bc84a

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/utilities/conversion.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ var p5 = require('../core/main');
2727
* var diameter = float(str);
2828
* ellipse(width / 2, height / 2, diameter, diameter);
2929
* </code></div>
30+
* <div class='norender'><code>
31+
* print(float('10.31')); // 10.31
32+
* print(float('Infinity')); // Infinity
33+
* print(float('-Infinity')); // -Infinity
34+
* </code></div>
3035
*
3136
* @alt
3237
* 20 by 20 white ellipse in the center of the canvas
@@ -57,6 +62,8 @@ p5.prototype.float = function(str) {
5762
* print(int(true)); // 1
5863
* print(int(false)); // 0
5964
* print(int([false, true, '10.3', 9.8])); // [0, 1, 10, 9]
65+
* print(int(Infinity)); // Infinity
66+
* print(int('-Infinity')); // -Infinity
6067
* </code></div>
6168
*/
6269
/**
@@ -66,7 +73,11 @@ p5.prototype.float = function(str) {
6673
*/
6774
p5.prototype.int = function(n, radix) {
6875
radix = radix || 10;
69-
if (typeof n === 'string') {
76+
if (n === Infinity || n === 'Infinity') {
77+
return Infinity;
78+
} else if (n === -Infinity || n === '-Infinity') {
79+
return -Infinity;
80+
} else if (typeof n === 'string') {
7081
return parseInt(n, radix);
7182
} else if (typeof n === 'number') {
7283
return n | 0;
@@ -252,6 +263,8 @@ p5.prototype.unchar = function(n) {
252263
* print(hex(255)); // "000000FF"
253264
* print(hex(255, 6)); // "0000FF"
254265
* print(hex([0, 127, 255], 6)); // [ "000000", "00007F", "0000FF" ]
266+
* print(Infinity); // "FFFFFFFF"
267+
* print(-Infinity); // "00000000"
255268
* </code></div>
256269
*/
257270
/**
@@ -266,6 +279,9 @@ p5.prototype.hex = function(n, digits) {
266279
return n.map(function(n) {
267280
return p5.prototype.hex(n, digits);
268281
});
282+
} else if (n === Infinity || n === -Infinity) {
283+
var c = n === Infinity ? 'F' : '0';
284+
return c.repeat(digits);
269285
} else if (typeof n === 'number') {
270286
if (n < 0) {
271287
n = 0xffffffff + n + 1;

test/unit/utilities/conversion.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ suite.only('Conversion', function() {
3838
assert.strictEqual(result, Infinity);
3939
});
4040

41+
test('should return -Infinity for -Infinity', function() {
42+
result = myp5.float(-Infinity);
43+
assert.strictEqual(result, -Infinity);
44+
});
45+
4146
test('should return array of floating points and Nan', function() {
4247
result = myp5.float(['1', '2.0', '3.1', 'giraffe']);
4348
assert.typeOf(result, 'Array');
@@ -77,6 +82,11 @@ suite.only('Conversion', function() {
7782
assert.strictEqual(result, Infinity);
7883
});
7984

85+
test('should return -Infinity for -Infinity', function() {
86+
result = myp5.int(-Infinity);
87+
assert.strictEqual(result, -Infinity);
88+
});
89+
8090
test('should convert float to its integer representation', function() {
8191
result = myp5.int('-1001.9');
8292
assert.strictEqual(result, -1001);
@@ -269,10 +279,16 @@ suite.only('Conversion', function() {
269279
assert.strictEqual(result, '00000041');
270280
});
271281

272-
test('should return Infinity for Infinity', function() {
282+
test('should return FFFFFFFF for Infinity', function() {
273283
result = myp5.hex(Infinity);
274284
assert.typeOf(result, 'Number');
275-
assert.strictEqual(result, Infinity);
285+
assert.strictEqual(result, 'FFFFFFFF');
286+
});
287+
288+
test('should return 00000000 for -Infinity', function() {
289+
result = myp5.hex(-Infinity);
290+
assert.typeOf(result, 'Number');
291+
assert.strictEqual(result, '00000000');
276292
});
277293

278294
test('should return array', function() {

0 commit comments

Comments
 (0)