forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.js
45 lines (38 loc) · 1.31 KB
/
float.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Sk.builtin.float_ = function (x) {
var tmp;
if (x === undefined) {
return new Sk.builtin.nmber(0.0, Sk.builtin.nmber.float$);
}
if (x instanceof Sk.builtin.str) {
if (x.v.match(/^-inf$/i)) {
tmp = -Infinity;
}
else if (x.v.match(/^[+]?inf$/i)) {
tmp = Infinity;
}
else if (x.v.match(/^[-+]?nan$/i)) {
tmp = NaN;
}
else if (!isNaN(x.v)) {
tmp = parseFloat(x.v);
}
else {
throw new Sk.builtin.ValueError("float: Argument: " + x.v + " is not number");
}
return new Sk.builtin.nmber(tmp, Sk.builtin.nmber.float$);
}
// Floats are just numbers
if (typeof x === "number" || x instanceof Sk.builtin.nmber ||
x instanceof Sk.builtin.lng) {
x = Sk.builtin.asnum$(x);
return new Sk.builtin.nmber(x, Sk.builtin.nmber.float$);
}
// Convert booleans
if (x instanceof Sk.builtin.bool) {
x = Sk.builtin.asnum$(x);
return new Sk.builtin.nmber(x, Sk.builtin.nmber.float$);
}
throw new Sk.builtin.TypeError("float() argument must be a string or a number");
};
Sk.builtin.float_.prototype.tp$name = "float";
Sk.builtin.float_.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("float", Sk.builtin.float_);