float.js.html 28 KB

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