-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloat.js
More file actions
896 lines (743 loc) · 25.6 KB
/
Copy pathfloat.js
File metadata and controls
896 lines (743 loc) · 25.6 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/**
* @namespace Sk.builtin
*/
/**
* @constructor
* Sk.builtin.float_
*
* @description
* Constructor for Python float. Also used for builtin float().
*
* @extends {Sk.builtin.numtype}
*
* @param {!(Object|number|string)} x Object or number to convert to Python float.
* @return {Sk.builtin.float_} Python float
*/
Sk.builtin.float_ = function (x) {
var tmp;
if (x === undefined) {
return new Sk.builtin.float_(0.0);
}
if (!(this instanceof Sk.builtin.float_)) {
return new Sk.builtin.float_(x);
}
if (x instanceof Sk.builtin.str) {
return Sk.builtin._str_to_float(x.v);
}
// Floats are just numbers
if (typeof x === "number" || x instanceof Sk.builtin.int_ || x instanceof Sk.builtin.lng || x instanceof Sk.builtin.float_) {
tmp = Sk.builtin.asnum$(x);
if (typeof tmp === "string") {
return Sk.builtin._str_to_float(tmp);
}
this.v = tmp;
return this;
}
// Convert booleans
if (x instanceof Sk.builtin.bool) {
this.v = Sk.builtin.asnum$(x);
return this;
}
// this is a special internal case
if(typeof x === "boolean") {
this.v = x ? 1.0 : 0.0;
return this;
}
if (typeof x === "string") {
this.v = parseFloat(x);
return this;
}
// try calling __float__
var special = Sk.abstr.lookupSpecial(x, "__float__");
if (special != null) {
// method on builtin, provide this arg
return Sk.misceval.callsim(special, x);
}
throw new Sk.builtin.TypeError("float() argument must be a string or a number");
};
Sk.abstr.setUpInheritance("float", Sk.builtin.float_, Sk.builtin.numtype);
Sk.builtin._str_to_float = function (str) {
var tmp;
if (str.match(/^-inf$/i)) {
tmp = -Infinity;
} else if (str.match(/^[+]?inf$/i)) {
tmp = Infinity;
} else if (str.match(/^[-+]?nan$/i)) {
tmp = NaN;
} else if (!isNaN(str)) {
tmp = parseFloat(str);
} else {
throw new Sk.builtin.ValueError("float: Argument: " + str + " is not number");
}
return new Sk.builtin.float_(tmp);
};
Sk.builtin.float_.prototype.nb$int_ = function () {
var v = this.v;
if (v < 0) {
v = Math.ceil(v);
} else {
v = Math.floor(v);
}
// this should take care of int/long fitting
return new Sk.builtin.int_(v);
};
Sk.builtin.float_.prototype.nb$float_ = function() {
return this;
};
Sk.builtin.float_.prototype.nb$lng = function () {
return new Sk.builtin.lng(this.v);
};
/**
* Checks for float subtypes, though skulpt does not allow to
* extend them for now.
*
* Javascript function, returns Javascript object.
* @param {Object} op The object to check as subtype.
* @return {boolean} true if op is a subtype of Sk.builtin.float_, false otherwise
*/
Sk.builtin.float_.PyFloat_Check = function (op) {
if (op === undefined) {
return false;
}
// this is a little bit hacky
// ToDo: subclassable builtins do not require this
if (Sk.builtin.checkNumber(op)) {
return true;
}
if (Sk.builtin.checkFloat(op)) {
return true;
}
if (Sk.builtin.issubclass(op.ob$type, Sk.builtin.float_)) {
return true;
}
return false;
};
/**
* Checks if ob is a Python float.
*
* This method is just a wrapper, but uses the correct cpython API name.
*
* Javascript function, returns Javascript object.
* @param {Object} op The object to check.
* @return {boolean} true if op is an instance of Sk.builtin.float_, false otherwise
*/
Sk.builtin.float_.PyFloat_Check_Exact = function (op) {
return Sk.builtin.checkFloat(op);
};
Sk.builtin.float_.PyFloat_AsDouble = function (op) {
var f; // nb_float;
var fo; // PyFloatObject *fo;
var val;
// it is a subclass or direct float
if (op && Sk.builtin.float_.PyFloat_Check(op)) {
return Sk.ffi.remapToJs(op);
}
if (op == null) {
throw new Error("bad argument for internal PyFloat_AsDouble function");
}
// check if special method exists (nb_float is not implemented in skulpt, hence we use __float__)
f = Sk.builtin.type.typeLookup(op.ob$type, "__float__");
if (f == null) {
throw new Sk.builtin.TypeError("a float is required");
}
// call internal float method
fo = Sk.misceval.callsim(f, op);
// return value of __float__ must be a python float
if (!Sk.builtin.float_.PyFloat_Check(fo)) {
throw new Sk.builtin.TypeError("nb_float should return float object");
}
val = Sk.ffi.remapToJs(fo);
return val;
};
/**
* Return this instance's Javascript value.
*
* Javascript function, returns Javascript object.
*
* @return {number} This instance's value.
*/
Sk.builtin.float_.prototype.tp$index = function () {
return this.v;
};
/** @override */
Sk.builtin.float_.prototype.tp$hash = function () {
//the hash of all numbers should be an int and since javascript doesn't really
//care every number can be an int.
return this.nb$int_();
};
/**
* Returns a copy of this instance.
*
* Javascript function, returns Python object.
*
* @return {Sk.builtin.float_} The copy
*/
Sk.builtin.float_.prototype.clone = function () {
return new Sk.builtin.float_(this.v);
};
/**
* Returns this instance's value as a string formatted using fixed-point notation.
*
* Javascript function, returns Javascript object.
*
* @param {Object|number} x The numer of digits to appear after the decimal point.
* @return {string} The string representation of this instance's value.
*/
Sk.builtin.float_.prototype.toFixed = function (x) {
x = Sk.builtin.asnum$(x);
return this.v.toFixed(x);
};
/** @override */
Sk.builtin.float_.prototype.nb$add = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
return new Sk.builtin.float_(this.v + other.v);
} else if (other instanceof Sk.builtin.lng) {
return new Sk.builtin.float_(this.v + parseFloat(other.str$(10, true)));
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_add = function (other) {
// Should not automatically call this.nb$add, as nb$add may have
// been overridden by a subclass
return Sk.builtin.float_.prototype.nb$add.call(this, other);
};
/** @override */
Sk.builtin.float_.prototype.nb$subtract = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
return new Sk.builtin.float_(this.v - other.v);
} else if (other instanceof Sk.builtin.lng) {
return new Sk.builtin.float_(this.v - parseFloat(other.str$(10, true)));
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_subtract = function (other) {
// Should not automatically call this.nb$add, as nb$add may have
// been overridden by a subclass
var negative_this = this.nb$negative();
return Sk.builtin.float_.prototype.nb$add.call(negative_this, other);
};
/** @override */
Sk.builtin.float_.prototype.nb$multiply = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
return new Sk.builtin.float_(this.v * other.v);
} else if (other instanceof Sk.builtin.lng) {
return new Sk.builtin.float_(this.v * parseFloat(other.str$(10, true)));
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_multiply = function (other) {
// Should not automatically call this.nb$multiply, as nb$multiply may have
// been overridden by a subclass
return Sk.builtin.float_.prototype.nb$multiply.call(this, other);
};
/** @override */
Sk.builtin.float_.prototype.nb$divide = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
if (other.v === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
if (this.v === Infinity) {
if (other.v === Infinity || other.v === -Infinity) {
return new Sk.builtin.float_(NaN);
} else if (other.nb$isnegative()) {
return new Sk.builtin.float_(-Infinity);
} else {
return new Sk.builtin.float_(Infinity);
}
}
if (this.v === -Infinity) {
if (other.v === Infinity || other.v === -Infinity) {
return new Sk.builtin.float_(NaN);
} else if (other.nb$isnegative()) {
return new Sk.builtin.float_(Infinity);
} else {
return new Sk.builtin.float_(-Infinity);
}
}
return new Sk.builtin.float_(this.v / other.v);
}
if (other instanceof Sk.builtin.lng) {
if (other.longCompare(Sk.builtin.biginteger.ZERO) === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
if (this.v === Infinity) {
if (other.nb$isnegative()) {
return new Sk.builtin.float_(-Infinity);
} else {
return new Sk.builtin.float_(Infinity);
}
}
if (this.v === -Infinity) {
if (other.nb$isnegative()) {
return new Sk.builtin.float_(Infinity);
} else {
return new Sk.builtin.float_(-Infinity);
}
}
return new Sk.builtin.float_(this.v / parseFloat(other.str$(10, true)));
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_divide = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng) {
other = new Sk.builtin.float_(other);
}
if (other instanceof Sk.builtin.float_) {
return other.nb$divide(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$floor_divide = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
if (this.v === Infinity || this.v === -Infinity) {
return new Sk.builtin.float_(NaN);
}
if (other.v === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
if (other.v === Infinity) {
if (this.nb$isnegative()) {
return new Sk.builtin.float_(-1);
} else {
return new Sk.builtin.float_(0);
}
}
if (other.v === -Infinity) {
if (this.nb$isnegative() || !this.nb$nonzero()) {
return new Sk.builtin.float_(0);
} else {
return new Sk.builtin.float_(-1);
}
}
return new Sk.builtin.float_(Math.floor(this.v / other.v));
}
if (other instanceof Sk.builtin.lng) {
if (other.longCompare(Sk.builtin.biginteger.ZERO) === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
if (this.v === Infinity || this.v === -Infinity) {
return new Sk.builtin.float_(NaN);
}
return new Sk.builtin.float_(Math.floor(this.v / parseFloat(other.str$(10, true))));
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_floor_divide = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng) {
other = new Sk.builtin.float_(other);
}
if (other instanceof Sk.builtin.float_) {
return other.nb$floor_divide(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$remainder = function (other) {
var thisAsLong;
var op2;
var tmp;
var result;
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
if (other.v === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
if (this.v === 0) {
return new Sk.builtin.float_(0);
}
if (other.v === Infinity) {
if (this.v === Infinity || this.v === -Infinity) {
return new Sk.builtin.float_(NaN);
} else if (this.nb$ispositive()) {
return new Sk.builtin.float_(this.v);
} else {
return new Sk.builtin.float_(Infinity);
}
}
// Javacript logic on negatives doesn't work for Python... do this instead
tmp = this.v % other.v;
if (this.v < 0) {
if (other.v > 0 && tmp < 0) {
tmp = tmp + other.v;
}
} else {
if (other.v < 0 && tmp !== 0) {
tmp = tmp + other.v;
}
}
if (other.v < 0 && tmp === 0) {
tmp = -0.0; // otherwise the sign gets lost by javascript modulo
} else if (tmp === 0 && Infinity/tmp === -Infinity) {
tmp = 0.0;
}
return new Sk.builtin.float_(tmp);
}
if (other instanceof Sk.builtin.lng) {
if (other.longCompare(Sk.builtin.biginteger.ZERO) === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
if (this.v === 0) {
return new Sk.builtin.float_(0);
}
op2 = parseFloat(other.str$(10, true));
tmp = this.v % op2;
if (tmp < 0) {
if (op2 > 0 && tmp !== 0) {
tmp = tmp + op2;
}
} else {
if (op2 < 0 && tmp !== 0) {
tmp = tmp + op2;
}
}
if (other.nb$isnegative() && tmp === 0) {
tmp = -0.0; // otherwise the sign gets lost by javascript modulo
} else if (tmp === 0 && Infinity/tmp === -Infinity) {
tmp = 0.0;
}
return new Sk.builtin.float_(tmp);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_remainder = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng) {
other = new Sk.builtin.float_(other);
}
if (other instanceof Sk.builtin.float_) {
return other.nb$remainder(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$divmod = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng) {
other = new Sk.builtin.float_(other);
}
if (other instanceof Sk.builtin.float_) {
return new Sk.builtin.tuple([
this.nb$floor_divide(other),
this.nb$remainder(other)
]);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_divmod = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng) {
other = new Sk.builtin.float_(other);
}
if (other instanceof Sk.builtin.float_) {
return new Sk.builtin.tuple([
other.nb$floor_divide(this),
other.nb$remainder(this)
]);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$power = function (other, mod) {
var thisAsLong;
var result;
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
if (this.v < 0 && other.v % 1 !== 0) {
throw new Sk.builtin.NegativePowerError("cannot raise a negative number to a fractional power");
}
if (this.v === 0 && other.v < 0) {
throw new Sk.builtin.NegativePowerError("cannot raise zero to a negative power");
}
result = new Sk.builtin.float_(Math.pow(this.v, other.v));
if ((Math.abs(result.v) === Infinity) &&
(Math.abs(this.v) !== Infinity) &&
(Math.abs(other.v) !== Infinity)) {
throw new Sk.builtin.OverflowError("Numerical result out of range");
}
return result;
}
if (other instanceof Sk.builtin.lng) {
if (this.v === 0 && other.longCompare(Sk.builtin.biginteger.ZERO) < 0) {
throw new Sk.builtin.NegativePowerError("cannot raise zero to a negative power");
}
return new Sk.builtin.float_(Math.pow(this.v, parseFloat(other.str$(10, true))));
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$reflected_power = function (n, mod) {
if (n instanceof Sk.builtin.int_ ||
n instanceof Sk.builtin.lng) {
n = new Sk.builtin.float_(n);
}
if (n instanceof Sk.builtin.float_) {
return n.nb$power(this, mod);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.float_.prototype.nb$abs = function () {
return new Sk.builtin.float_(Math.abs(this.v));
};
/** @override */
Sk.builtin.float_.prototype.nb$inplace_add = Sk.builtin.float_.prototype.nb$add;
/** @override */
Sk.builtin.float_.prototype.nb$inplace_subtract = Sk.builtin.float_.prototype.nb$subtract;
/** @override */
Sk.builtin.float_.prototype.nb$inplace_multiply = Sk.builtin.float_.prototype.nb$multiply;
/** @override */
Sk.builtin.float_.prototype.nb$inplace_divide = Sk.builtin.float_.prototype.nb$divide;
/** @override */
Sk.builtin.float_.prototype.nb$inplace_remainder = Sk.builtin.float_.prototype.nb$remainder;
/** @override */
Sk.builtin.float_.prototype.nb$inplace_floor_divide = Sk.builtin.float_.prototype.nb$floor_divide;
/** @override */
Sk.builtin.float_.prototype.nb$inplace_power = Sk.builtin.float_.prototype.nb$power;
/**
* @override
*
* @return {Sk.builtin.float_} A copy of this instance with the value negated.
*/
Sk.builtin.float_.prototype.nb$negative = function () {
return new Sk.builtin.float_(-this.v);
};
/** @override */
Sk.builtin.float_.prototype.nb$positive = function () {
return this.clone();
};
/** @override */
Sk.builtin.float_.prototype.nb$nonzero = function () {
return this.v !== 0;
};
/** @override */
Sk.builtin.float_.prototype.nb$isnegative = function () {
return this.v < 0;
};
/** @override */
Sk.builtin.float_.prototype.nb$ispositive = function () {
return this.v >= 0;
};
/**
* Compare this instance's value to another Python object's value.
*
* Returns NotImplemented if comparison between float and other type is unsupported.
*
* Javscript function, returns Javascript object or Sk.builtin.NotImplemented.
*
* @return {(number|Sk.builtin.NotImplemented)} negative if this < other, zero if this == other, positive if this > other
*/
Sk.builtin.float_.prototype.numberCompare = function (other) {
var diff;
var tmp;
var thisAsLong;
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.float_) {
if (this.v == Infinity && other.v == Infinity) {
return 0;
}
if (this.v == -Infinity && other.v == -Infinity) {
return 0;
}
return this.v - other.v;
}
if (other instanceof Sk.builtin.lng) {
if (this.v % 1 === 0) {
thisAsLong = new Sk.builtin.lng(this.v);
tmp = thisAsLong.longCompare(other);
return tmp;
}
diff = this.nb$subtract(other);
if (diff instanceof Sk.builtin.float_) {
return diff.v;
} else if (diff instanceof Sk.builtin.lng) {
return diff.longCompare(Sk.builtin.biginteger.ZERO);
}
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
// Despite what jshint may want us to do, these two functions need to remain
// as == and != Unless you modify the logic of numberCompare do not change
// these.
/** @override */
Sk.builtin.float_.prototype.ob$eq = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) == 0); //jshint ignore:line
} else if (other instanceof Sk.builtin.none) {
return Sk.builtin.bool.false$;
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.float_.prototype.ob$ne = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) != 0); //jshint ignore:line
} else if (other instanceof Sk.builtin.none) {
return Sk.builtin.bool.true$;
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.float_.prototype.ob$lt = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) < 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.float_.prototype.ob$le = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) <= 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.float_.prototype.ob$gt = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) > 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.float_.prototype.ob$ge = function (other) {
if (other instanceof Sk.builtin.int_ ||
other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) >= 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/**
* Round this instance to a given number of digits, or zero if omitted.
*
* Implements `__round__` dunder method.
*
* Javascript function, returns Python object.
*
* @param {Sk.builtin.int_} self This instance.
* @param {Object|number=} ndigits The number of digits after the decimal point to which to round.
* @return {Sk.builtin.float_} The rounded float.
*/
Sk.builtin.float_.prototype.__round__ = function (self, ndigits) {
Sk.builtin.pyCheckArgs("__round__", arguments, 1, 2);
var result, multiplier, number;
if ((ndigits !== undefined) && !Sk.misceval.isIndex(ndigits)) {
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(ndigits) + "' object cannot be interpreted as an index");
}
if (ndigits === undefined) {
ndigits = 0;
}
number = Sk.builtin.asnum$(self);
ndigits = Sk.misceval.asIndex(ndigits);
multiplier = Math.pow(10, ndigits);
result = Math.round(number * multiplier) / multiplier;
return new Sk.builtin.float_(result);
};
Sk.builtin.float_.prototype.conjugate = new Sk.builtin.func(function (self) {
return new Sk.builtin.float_(self.v);
});
/** @override */
Sk.builtin.float_.prototype["$r"] = function () {
return new Sk.builtin.str(this.str$(10, true));
};
/**
* Return the string representation of this instance.
*
* Javascript function, returns Python object.
*
* @return {Sk.builtin.str} The Python string representation of this instance.
*/
Sk.builtin.float_.prototype.tp$str = function () {
return new Sk.builtin.str(this.str$(10, true));
};
/**
* Convert this instance's value to a Javascript string.
*
* Javascript function, returns Javascript object.
*
* @param {number} base The base of the value.
* @param {boolean} sign true if the value should be signed, false otherwise.
* @return {string} The Javascript string representation of this instance.
*/
Sk.builtin.float_.prototype.str$ = function (base, sign) {
var post;
var pre;
var idx;
var tmp;
var work;
if (isNaN(this.v)) {
return "nan";
}
if (sign === undefined) {
sign = true;
}
if (this.v == Infinity) {
return "inf";
}
if (this.v == -Infinity && sign) {
return "-inf";
}
if (this.v == -Infinity && !sign) {
return "inf";
}
work = sign ? this.v : Math.abs(this.v);
if (base === undefined || base === 10) {
tmp = work.toPrecision(12);
// transform fractions with 4 or more leading zeroes into exponents
idx = tmp.indexOf(".");
pre = work.toString().slice(0, idx);
post = work.toString().slice(idx);
if (pre.match(/^-?0$/) && post.slice(1).match(/^0{4,}/)) {
if (tmp.length < 12) {
tmp = work.toExponential();
} else {
tmp = work.toExponential(11);
}
}
if (tmp.indexOf("e") < 0 && tmp.indexOf(".") >= 0) {
while (tmp.charAt(tmp.length-1) == "0") {
tmp = tmp.substring(0,tmp.length-1);
}
if (tmp.charAt(tmp.length-1) == ".") {
tmp = tmp + "0";
}
}
tmp = tmp.replace(new RegExp("\\.0+e"), "e", "i");
// make exponent two digits instead of one (ie e+09 not e+9)
tmp = tmp.replace(/(e[-+])([1-9])$/, "$10$2");
// remove trailing zeroes before the exponent
tmp = tmp.replace(/0+(e.*)/, "$1");
} else {
tmp = work.toString(base);
}
// restore negative zero sign
if(this.v === 0 && 1/this.v === -Infinity) {
tmp = "-" + tmp;
}
if (tmp.indexOf(".") < 0 && tmp.indexOf("E") < 0 && tmp.indexOf("e") < 0) {
tmp = tmp + ".0";
}
return tmp;
};