当前位置:   article > 正文

js BigDecimal

js bigdecimal

学过java的同学应该都知道,java中有一个BigDecimal 是专门用来算金额的,今天这里的BigDecimal是从java翻译成js的。

先看用法:

加法(四舍五入保留两位小数):

  1. var a = new BigDecimal("2.555555").add(new BigDecimal("5.222222")).setScale(2, MathContext.ROUND_HALF_UP).toString();
  2. console.log(a);

减法: 

new BigDecimal("2.40").subtract(new BigDecimal("2"))

乘法:

new BigDecimal("2.40").multiply(new BigDecimal("2"))

除法:

new BigDecimal("2.40").divide(new BigDecimal("2"), def)

下面来看看源代码:

  1. /** @license Copyright (c) 2012 Daniel Trebbien and other contributors
  2. Portions Copyright (c) 2003 STZ-IDA and PTV AG, Karlsruhe, Germany
  3. Portions Copyright (c) 1995-2001 International Business Machines Corporation and others
  4. All rights reserved.
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, provided that the above copyright notice(s) and this permission notice appear in all copies of the Software and that both the above copyright notice(s) and this permission notice appear in supporting documentation.
  6. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  7. Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder.
  8. */
  9. (function () {
  10. var MathContext = (function () {
  11. /* Generated from 'MathContext.nrx' 8 Sep 2000 11:07:48 [v2.00] */
  12. /* Options: Binary Comments Crossref Format Java Logo Strictargs Strictcase Trace2 Verbose3 */
  13. //--package com.ibm.icu.math;
  14. /* ------------------------------------------------------------------ */
  15. /* MathContext -- Math context settings */
  16. /* ------------------------------------------------------------------ */
  17. /* Copyright IBM Corporation, 1997, 2000. All Rights Reserved. */
  18. /* */
  19. /* The MathContext object encapsulates the settings used by the */
  20. /* BigDecimal class; it could also be used by other arithmetics. */
  21. /* ------------------------------------------------------------------ */
  22. /* Notes: */
  23. /* */
  24. /* 1. The properties are checked for validity on construction, so */
  25. /* the BigDecimal class may assume that they are correct. */
  26. /* ------------------------------------------------------------------ */
  27. /* Author: Mike Cowlishaw */
  28. /* 1997.09.03 Initial version (edited from netrexx.lang.RexxSet) */
  29. /* 1997.09.12 Add lostDigits property */
  30. /* 1998.05.02 Make the class immutable and final; drop set methods */
  31. /* 1998.06.05 Add Round (rounding modes) property */
  32. /* 1998.06.25 Rename from DecimalContext; allow digits=0 */
  33. /* 1998.10.12 change to com.ibm.icu.math package */
  34. /* 1999.02.06 add javadoc comments */
  35. /* 1999.03.05 simplify; changes from discussion with J. Bloch */
  36. /* 1999.03.13 1.00 release to IBM Centre for Java Technology */
  37. /* 1999.07.10 1.04 flag serialization unused */
  38. /* 2000.01.01 1.06 copyright update */
  39. /* ------------------------------------------------------------------ */
  40. /* JavaScript conversion (c) 2003 STZ-IDA and PTV AG, Karlsruhe, Germany */
  41. /**
  42. * The <code>MathContext</code> immutable class encapsulates the
  43. * settings understood by the operator methods of the {@link BigDecimal}
  44. * class (and potentially other classes). Operator methods are those
  45. * that effect an operation on a number or a pair of numbers.
  46. * <p>
  47. * The settings, which are not base-dependent, comprise:
  48. * <ol>
  49. * <li><code>digits</code>:
  50. * the number of digits (precision) to be used for an operation
  51. * <li><code>form</code>:
  52. * the form of any exponent that results from the operation
  53. * <li><code>lostDigits</code>:
  54. * whether checking for lost digits is enabled
  55. * <li><code>roundingMode</code>:
  56. * the algorithm to be used for rounding.
  57. * </ol>
  58. * <p>
  59. * When provided, a <code>MathContext</code> object supplies the
  60. * settings for an operation directly.
  61. * <p>
  62. * When <code>MathContext.DEFAULT</code> is provided for a
  63. * <code>MathContext</code> parameter then the default settings are used
  64. * (<code>9, SCIENTIFIC, false, ROUND_HALF_UP</code>).
  65. * <p>
  66. * In the <code>BigDecimal</code> class, all methods which accept a
  67. * <code>MathContext</code> object defaults) also have a version of the
  68. * method which does not accept a MathContext parameter. These versions
  69. * carry out unlimited precision fixed point arithmetic (as though the
  70. * settings were (<code>0, PLAIN, false, ROUND_HALF_UP</code>).
  71. * <p>
  72. * The instance variables are shared with default access (so they are
  73. * directly accessible to the <code>BigDecimal</code> class), but must
  74. * never be changed.
  75. * <p>
  76. * The rounding mode constants have the same names and values as the
  77. * constants of the same name in <code>java.math.BigDecimal</code>, to
  78. * maintain compatibility with earlier versions of
  79. * <code>BigDecimal</code>.
  80. *
  81. * @see BigDecimal
  82. * @author Mike Cowlishaw
  83. * @stable ICU 2.0
  84. */
  85. //--public final class MathContext implements java.io.Serializable{
  86. //--private static final java.lang.String $0="MathContext.nrx";
  87. //-- methods
  88. MathContext.prototype.getDigits = getDigits;
  89. MathContext.prototype.getForm = getForm;
  90. MathContext.prototype.getLostDigits = getLostDigits;
  91. MathContext.prototype.getRoundingMode = getRoundingMode;
  92. MathContext.prototype.toString = toString;
  93. MathContext.prototype.isValidRound = isValidRound;
  94. /* ----- Properties ----- */
  95. /* properties public constant */
  96. /**
  97. * Plain (fixed point) notation, without any exponent.
  98. * Used as a setting to control the form of the result of a
  99. * <code>BigDecimal</code> operation.
  100. * A zero result in plain form may have a decimal part of one or
  101. * more zeros.
  102. *
  103. * @see #ENGINEERING
  104. * @see #SCIENTIFIC
  105. * @stable ICU 2.0
  106. */
  107. //--public static final int PLAIN=0; // [no exponent]
  108. MathContext.PLAIN = MathContext.prototype.PLAIN = 0; // [no exponent]
  109. /**
  110. * Standard floating point notation (with scientific exponential
  111. * format, where there is one digit before any decimal point).
  112. * Used as a setting to control the form of the result of a
  113. * <code>BigDecimal</code> operation.
  114. * A zero result in plain form may have a decimal part of one or
  115. * more zeros.
  116. *
  117. * @see #ENGINEERING
  118. * @see #PLAIN
  119. * @stable ICU 2.0
  120. */
  121. //--public static final int SCIENTIFIC=1; // 1 digit before .
  122. MathContext.SCIENTIFIC = MathContext.prototype.SCIENTIFIC = 1; // 1 digit before .
  123. /**
  124. * Standard floating point notation (with engineering exponential
  125. * format, where the power of ten is a multiple of 3).
  126. * Used as a setting to control the form of the result of a
  127. * <code>BigDecimal</code> operation.
  128. * A zero result in plain form may have a decimal part of one or
  129. * more zeros.
  130. *
  131. * @see #PLAIN
  132. * @see #SCIENTIFIC
  133. * @stable ICU 2.0
  134. */
  135. //--public static final int ENGINEERING=2; // 1-3 digits before .
  136. MathContext.ENGINEERING = MathContext.prototype.ENGINEERING = 2; // 1-3 digits before .
  137. // The rounding modes match the original BigDecimal class values
  138. /**
  139. * Rounding mode to round to a more positive number.
  140. * Used as a setting to control the rounding mode used during a
  141. * <code>BigDecimal</code> operation.
  142. * <p>
  143. * If any of the discarded digits are non-zero then the result
  144. * should be rounded towards the next more positive digit.
  145. * @stable ICU 2.0
  146. */
  147. //--public static final int ROUND_CEILING=2;
  148. MathContext.ROUND_CEILING = MathContext.prototype.ROUND_CEILING = 2;
  149. /**
  150. * Rounding mode to round towards zero.
  151. * Used as a setting to control the rounding mode used during a
  152. * <code>BigDecimal</code> operation.
  153. * <p>
  154. * All discarded digits are ignored (truncated). The result is
  155. * neither incremented nor decremented.
  156. * @stable ICU 2.0
  157. */
  158. //--public static final int ROUND_DOWN=1;
  159. MathContext.ROUND_DOWN = MathContext.prototype.ROUND_DOWN = 1;
  160. /**
  161. * Rounding mode to round to a more negative number.
  162. * Used as a setting to control the rounding mode used during a
  163. * <code>BigDecimal</code> operation.
  164. * <p>
  165. * If any of the discarded digits are non-zero then the result
  166. * should be rounded towards the next more negative digit.
  167. * @stable ICU 2.0
  168. */
  169. //--public static final int ROUND_FLOOR=3;
  170. MathContext.ROUND_FLOOR = MathContext.prototype.ROUND_FLOOR = 3;
  171. /**
  172. * Rounding mode to round to nearest neighbor, where an equidistant
  173. * value is rounded down.
  174. * Used as a setting to control the rounding mode used during a
  175. * <code>BigDecimal</code> operation.
  176. * <p>
  177. * If the discarded digits represent greater than half (0.5 times)
  178. * the value of a one in the next position then the result should be
  179. * rounded up (away from zero). Otherwise the discarded digits are
  180. * ignored.
  181. * @stable ICU 2.0
  182. */
  183. //--public static final int ROUND_HALF_DOWN=5;
  184. MathContext.ROUND_HALF_DOWN = MathContext.prototype.ROUND_HALF_DOWN = 5;
  185. /**
  186. * Rounding mode to round to nearest neighbor, where an equidistant
  187. * value is rounded to the nearest even neighbor.
  188. * Used as a setting to control the rounding mode used during a
  189. * <code>BigDecimal</code> operation.
  190. * <p>
  191. * If the discarded digits represent greater than half (0.5 times)
  192. * the value of a one in the next position then the result should be
  193. * rounded up (away from zero). If they represent less than half,
  194. * then the result should be rounded down.
  195. * <p>
  196. * Otherwise (they represent exactly half) the result is rounded
  197. * down if its rightmost digit is even, or rounded up if its
  198. * rightmost digit is odd (to make an even digit).
  199. * @stable ICU 2.0
  200. */
  201. //--public static final int ROUND_HALF_EVEN=6;
  202. MathContext.ROUND_HALF_EVEN = MathContext.prototype.ROUND_HALF_EVEN = 6;
  203. /**
  204. * Rounding mode to round to nearest neighbor, where an equidistant
  205. * value is rounded up.
  206. * Used as a setting to control the rounding mode used during a
  207. * <code>BigDecimal</code> operation.
  208. * <p>
  209. * If the discarded digits represent greater than or equal to half
  210. * (0.5 times) the value of a one in the next position then the result
  211. * should be rounded up (away from zero). Otherwise the discarded
  212. * digits are ignored.
  213. * @stable ICU 2.0
  214. */
  215. //--public static final int ROUND_HALF_UP=4;
  216. MathContext.ROUND_HALF_UP = MathContext.prototype.ROUND_HALF_UP = 4;
  217. /**
  218. * Rounding mode to assert that no rounding is necessary.
  219. * Used as a setting to control the rounding mode used during a
  220. * <code>BigDecimal</code> operation.
  221. * <p>
  222. * Rounding (potential loss of information) is not permitted.
  223. * If any of the discarded digits are non-zero then an
  224. * <code>ArithmeticException</code> should be thrown.
  225. * @stable ICU 2.0
  226. */
  227. //--public static final int ROUND_UNNECESSARY=7;
  228. MathContext.ROUND_UNNECESSARY = MathContext.prototype.ROUND_UNNECESSARY = 7;
  229. /**
  230. * Rounding mode to round away from zero.
  231. * Used as a setting to control the rounding mode used during a
  232. * <code>BigDecimal</code> operation.
  233. * <p>
  234. * If any of the discarded digits are non-zero then the result will
  235. * be rounded up (away from zero).
  236. * @stable ICU 2.0
  237. */
  238. //--public static final int ROUND_UP=0;
  239. MathContext.ROUND_UP = MathContext.prototype.ROUND_UP = 0;
  240. /* properties shared */
  241. /**
  242. * The number of digits (precision) to be used for an operation.
  243. * A value of 0 indicates that unlimited precision (as many digits
  244. * as are required) will be used.
  245. * <p>
  246. * The {@link BigDecimal} operator methods use this value to
  247. * determine the precision of results.
  248. * Note that leading zeros (in the integer part of a number) are
  249. * never significant.
  250. * <p>
  251. * <code>digits</code> will always be non-negative.
  252. *
  253. * @serial
  254. */
  255. //--int digits;
  256. /**
  257. * The form of results from an operation.
  258. * <p>
  259. * The {@link BigDecimal} operator methods use this value to
  260. * determine the form of results, in particular whether and how
  261. * exponential notation should be used.
  262. *
  263. * @see #ENGINEERING
  264. * @see #PLAIN
  265. * @see #SCIENTIFIC
  266. * @serial
  267. */
  268. //--int form; // values for this must fit in a byte
  269. /**
  270. * Controls whether lost digits checking is enabled for an
  271. * operation.
  272. * Set to <code>true</code> to enable checking, or
  273. * to <code>false</code> to disable checking.
  274. * <p>
  275. * When enabled, the {@link BigDecimal} operator methods check
  276. * the precision of their operand or operands, and throw an
  277. * <code>ArithmeticException</code> if an operand is more precise
  278. * than the digits setting (that is, digits would be lost).
  279. * When disabled, operands are rounded to the specified digits.
  280. *
  281. * @serial
  282. */
  283. //--boolean lostDigits;
  284. /**
  285. * The rounding algorithm to be used for an operation.
  286. * <p>
  287. * The {@link BigDecimal} operator methods use this value to
  288. * determine the algorithm to be used when non-zero digits have to
  289. * be discarded in order to reduce the precision of a result.
  290. * The value must be one of the public constants whose name starts
  291. * with <code>ROUND_</code>.
  292. *
  293. * @see #ROUND_CEILING
  294. * @see #ROUND_DOWN
  295. * @see #ROUND_FLOOR
  296. * @see #ROUND_HALF_DOWN
  297. * @see #ROUND_HALF_EVEN
  298. * @see #ROUND_HALF_UP
  299. * @see #ROUND_UNNECESSARY
  300. * @see #ROUND_UP
  301. * @serial
  302. */
  303. //--int roundingMode;
  304. /* properties private constant */
  305. // default settings
  306. //--private static final int DEFAULT_FORM=SCIENTIFIC;
  307. //--private static final int DEFAULT_DIGITS=9;
  308. //--private static final boolean DEFAULT_LOSTDIGITS=false;
  309. //--private static final int DEFAULT_ROUNDINGMODE=ROUND_HALF_UP;
  310. MathContext.prototype.DEFAULT_FORM=MathContext.prototype.SCIENTIFIC;
  311. MathContext.prototype.DEFAULT_DIGITS=9;
  312. MathContext.prototype.DEFAULT_LOSTDIGITS=false;
  313. MathContext.prototype.DEFAULT_ROUNDINGMODE=MathContext.prototype.ROUND_HALF_UP;
  314. /* properties private constant */
  315. //--private static final int MIN_DIGITS=0; // smallest value for DIGITS.
  316. //--private static final int MAX_DIGITS=999999999; // largest value for DIGITS. If increased,
  317. MathContext.prototype.MIN_DIGITS=0; // smallest value for DIGITS.
  318. MathContext.prototype.MAX_DIGITS=999999999; // largest value for DIGITS. If increased,
  319. // the BigDecimal class may need update.
  320. // list of valid rounding mode values, most common two first
  321. //--private static final int ROUNDS[]=new int[]{ROUND_HALF_UP,ROUND_UNNECESSARY,ROUND_CEILING,ROUND_DOWN,ROUND_FLOOR,ROUND_HALF_DOWN,ROUND_HALF_EVEN,ROUND_UP};
  322. MathContext.prototype.ROUNDS=new Array(MathContext.prototype.ROUND_HALF_UP,MathContext.prototype.ROUND_UNNECESSARY,MathContext.prototype.ROUND_CEILING,MathContext.prototype.ROUND_DOWN,MathContext.prototype.ROUND_FLOOR,MathContext.prototype.ROUND_HALF_DOWN,MathContext.prototype.ROUND_HALF_EVEN,MathContext.prototype.ROUND_UP);
  323. //--private static final java.lang.String ROUNDWORDS[]=new java.lang.String[]{"ROUND_HALF_UP","ROUND_UNNECESSARY","ROUND_CEILING","ROUND_DOWN","ROUND_FLOOR","ROUND_HALF_DOWN","ROUND_HALF_EVEN","ROUND_UP"}; // matching names of the ROUNDS values
  324. MathContext.prototype.ROUNDWORDS=new Array("ROUND_HALF_UP","ROUND_UNNECESSARY","ROUND_CEILING","ROUND_DOWN","ROUND_FLOOR","ROUND_HALF_DOWN","ROUND_HALF_EVEN","ROUND_UP"); // matching names of the ROUNDS values
  325. /* properties private constant unused */
  326. // Serialization version
  327. //--private static final long serialVersionUID=7163376998892515376L;
  328. /* properties public constant */
  329. /**
  330. * A <code>MathContext</code> object initialized to the default
  331. * settings for general-purpose arithmetic. That is,
  332. * <code>digits=9 form=SCIENTIFIC lostDigits=false
  333. * roundingMode=ROUND_HALF_UP</code>.
  334. *
  335. * @see #SCIENTIFIC
  336. * @see #ROUND_HALF_UP
  337. * @stable ICU 2.0
  338. */
  339. //--public static final com.ibm.icu.math.MathContext DEFAULT=new com.ibm.icu.math.MathContext(DEFAULT_DIGITS,DEFAULT_FORM,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
  340. MathContext.prototype.DEFAULT=new MathContext(MathContext.prototype.DEFAULT_DIGITS,MathContext.prototype.DEFAULT_FORM,MathContext.prototype.DEFAULT_LOSTDIGITS,MathContext.prototype.DEFAULT_ROUNDINGMODE);
  341. /* ----- Constructors ----- */
  342. /**
  343. * Constructs a new <code>MathContext</code> with a specified
  344. * precision.
  345. * The other settings are set to the default values
  346. * (see {@link #DEFAULT}).
  347. *
  348. * An <code>IllegalArgumentException</code> is thrown if the
  349. * <code>setdigits</code> parameter is out of range
  350. * (<0 or >999999999).
  351. *
  352. * @param setdigits The <code>int</code> digits setting
  353. * for this <code>MathContext</code>.
  354. * @throws IllegalArgumentException parameter out of range.
  355. * @stable ICU 2.0
  356. */
  357. //--public MathContext(int setdigits){
  358. //-- this(setdigits,DEFAULT_FORM,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
  359. //-- return;}
  360. /**
  361. * Constructs a new <code>MathContext</code> with a specified
  362. * precision and form.
  363. * The other settings are set to the default values
  364. * (see {@link #DEFAULT}).
  365. *
  366. * An <code>IllegalArgumentException</code> is thrown if the
  367. * <code>setdigits</code> parameter is out of range
  368. * (<0 or >999999999), or if the value given for the
  369. * <code>setform</code> parameter is not one of the appropriate
  370. * constants.
  371. *
  372. * @param setdigits The <code>int</code> digits setting
  373. * for this <code>MathContext</code>.
  374. * @param setform The <code>int</code> form setting
  375. * for this <code>MathContext</code>.
  376. * @throws IllegalArgumentException parameter out of range.
  377. * @stable ICU 2.0
  378. */
  379. //--public MathContext(int setdigits,int setform){
  380. //-- this(setdigits,setform,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
  381. //-- return;}
  382. /**
  383. * Constructs a new <code>MathContext</code> with a specified
  384. * precision, form, and lostDigits setting.
  385. * The roundingMode setting is set to its default value
  386. * (see {@link #DEFAULT}).
  387. *
  388. * An <code>IllegalArgumentException</code> is thrown if the
  389. * <code>setdigits</code> parameter is out of range
  390. * (<0 or >999999999), or if the value given for the
  391. * <code>setform</code> parameter is not one of the appropriate
  392. * constants.
  393. *
  394. * @param setdigits The <code>int</code> digits setting
  395. * for this <code>MathContext</code>.
  396. * @param setform The <code>int</code> form setting
  397. * for this <code>MathContext</code>.
  398. * @param setlostdigits The <code>boolean</code> lostDigits
  399. * setting for this <code>MathContext</code>.
  400. * @throws IllegalArgumentException parameter out of range.
  401. * @stable ICU 2.0
  402. */
  403. //--public MathContext(int setdigits,int setform,boolean setlostdigits){
  404. //-- this(setdigits,setform,setlostdigits,DEFAULT_ROUNDINGMODE);
  405. //-- return;}
  406. /**
  407. * Constructs a new <code>MathContext</code> with a specified
  408. * precision, form, lostDigits, and roundingMode setting.
  409. *
  410. * An <code>IllegalArgumentException</code> is thrown if the
  411. * <code>setdigits</code> parameter is out of range
  412. * (<0 or >999999999), or if the value given for the
  413. * <code>setform</code> or <code>setroundingmode</code> parameters is
  414. * not one of the appropriate constants.
  415. *
  416. * @param setdigits The <code>int</code> digits setting
  417. * for this <code>MathContext</code>.
  418. * @param setform The <code>int</code> form setting
  419. * for this <code>MathContext</code>.
  420. * @param setlostdigits The <code>boolean</code> lostDigits
  421. * setting for this <code>MathContext</code>.
  422. * @param setroundingmode The <code>int</code> roundingMode setting
  423. * for this <code>MathContext</code>.
  424. * @throws IllegalArgumentException parameter out of range.
  425. * @stable ICU 2.0
  426. */
  427. //--public MathContext(int setdigits,int setform,boolean setlostdigits,int setroundingmode){super();
  428. function MathContext() {
  429. //-- members
  430. this.digits = 0;
  431. this.form = 0; // values for this must fit in a byte
  432. this.lostDigits = false;
  433. this.roundingMode = 0;
  434. //-- overloaded ctor
  435. var setform = this.DEFAULT_FORM;
  436. var setlostdigits = this.DEFAULT_LOSTDIGITS;
  437. var setroundingmode = this.DEFAULT_ROUNDINGMODE;
  438. if (MathContext.arguments.length == 4)
  439. {
  440. setform = MathContext.arguments[1];
  441. setlostdigits = MathContext.arguments[2];
  442. setroundingmode = MathContext.arguments[3];
  443. }
  444. else if (MathContext.arguments.length == 3)
  445. {
  446. setform = MathContext.arguments[1];
  447. setlostdigits = MathContext.arguments[2];
  448. }
  449. else if (MathContext.arguments.length == 2)
  450. {
  451. setform = MathContext.arguments[1];
  452. }
  453. else if (MathContext.arguments.length != 1)
  454. {
  455. throw "MathContext(): " + MathContext.arguments.length + " arguments given; expected 1 to 4";
  456. }
  457. var setdigits = MathContext.arguments[0];
  458. // set values, after checking
  459. if (setdigits!=this.DEFAULT_DIGITS)
  460. {
  461. if (setdigits<this.MIN_DIGITS)
  462. throw "MathContext(): Digits too small: "+setdigits;
  463. if (setdigits>this.MAX_DIGITS)
  464. throw "MathContext(): Digits too large: "+setdigits;
  465. }
  466. {/*select*/
  467. if (setform==this.SCIENTIFIC)
  468. {} // [most common]
  469. else if (setform==this.ENGINEERING)
  470. {}
  471. else if (setform==this.PLAIN)
  472. {}
  473. else{
  474. throw "MathContext() Bad form value: "+setform;
  475. }
  476. }
  477. if ((!(this.isValidRound(setroundingmode))))
  478. throw "MathContext(): Bad roundingMode value: "+setroundingmode;
  479. this.digits=setdigits;
  480. this.form=setform;
  481. this.lostDigits=setlostdigits; // [no bad value possible]
  482. this.roundingMode=setroundingmode;
  483. return;}
  484. /**
  485. * Returns the digits setting.
  486. * This value is always non-negative.
  487. *
  488. * @return an <code>int</code> which is the value of the digits
  489. * setting
  490. * @stable ICU 2.0
  491. */
  492. //--public int getDigits(){
  493. function getDigits() {
  494. return this.digits;
  495. }
  496. /**
  497. * Returns the form setting.
  498. * This will be one of
  499. * {@link #ENGINEERING},
  500. * {@link #PLAIN}, or
  501. * {@link #SCIENTIFIC}.
  502. *
  503. * @return an <code>int</code> which is the value of the form setting
  504. * @stable ICU 2.0
  505. */
  506. //--public int getForm(){
  507. function getForm() {
  508. return this.form;
  509. }
  510. /**
  511. * Returns the lostDigits setting.
  512. * This will be either <code>true</code> (enabled) or
  513. * <code>false</code> (disabled).
  514. *
  515. * @return a <code>boolean</code> which is the value of the lostDigits
  516. * setting
  517. * @stable ICU 2.0
  518. */
  519. //--public boolean getLostDigits(){
  520. function getLostDigits() {
  521. return this.lostDigits;
  522. }
  523. /**
  524. * Returns the roundingMode setting.
  525. * This will be one of
  526. * {@link #ROUND_CEILING},
  527. * {@link #ROUND_DOWN},
  528. * {@link #ROUND_FLOOR},
  529. * {@link #ROUND_HALF_DOWN},
  530. * {@link #ROUND_HALF_EVEN},
  531. * {@link #ROUND_HALF_UP},
  532. * {@link #ROUND_UNNECESSARY}, or
  533. * {@link #ROUND_UP}.
  534. *
  535. * @return an <code>int</code> which is the value of the roundingMode
  536. * setting
  537. * @stable ICU 2.0
  538. */
  539. //--public int getRoundingMode(){
  540. function getRoundingMode() {
  541. return this.roundingMode;
  542. }
  543. /** Returns the <code>MathContext</code> as a readable string.
  544. * The <code>String</code> returned represents the settings of the
  545. * <code>MathContext</code> object as four blank-delimited words
  546. * separated by a single blank and with no leading or trailing blanks,
  547. * as follows:
  548. * <ol>
  549. * <li>
  550. * <code>digits=</code>, immediately followed by
  551. * the value of the digits setting as a numeric word.
  552. * <li>
  553. * <code>form=</code>, immediately followed by
  554. * the value of the form setting as an uppercase word
  555. * (one of <code>SCIENTIFIC</code>, <code>PLAIN</code>, or
  556. * <code>ENGINEERING</code>).
  557. * <li>
  558. * <code>lostDigits=</code>, immediately followed by
  559. * the value of the lostDigits setting
  560. * (<code>1</code> if enabled, <code>0</code> if disabled).
  561. * <li>
  562. * <code>roundingMode=</code>, immediately followed by
  563. * the value of the roundingMode setting as a word.
  564. * This word will be the same as the name of the corresponding public
  565. * constant.
  566. * </ol>
  567. * <p>
  568. * For example:
  569. * <br><code>
  570. * digits=9 form=SCIENTIFIC lostDigits=0 roundingMode=ROUND_HALF_UP
  571. * </code>
  572. * <p>
  573. * Additional words may be appended to the result of
  574. * <code>toString</code> in the future if more properties are added
  575. * to the class.
  576. *
  577. * @return a <code>String</code> representing the context settings.
  578. * @stable ICU 2.0
  579. */
  580. //--public java.lang.String toString(){
  581. function toString() {
  582. //--java.lang.String formstr=null;
  583. var formstr=null;
  584. //--int r=0;
  585. var r=0;
  586. //--java.lang.String roundword=null;
  587. var roundword=null;
  588. {/*select*/
  589. if (this.form==this.SCIENTIFIC)
  590. formstr="SCIENTIFIC";
  591. else if (this.form==this.ENGINEERING)
  592. formstr="ENGINEERING";
  593. else{
  594. formstr="PLAIN";/* form=PLAIN */
  595. }
  596. }
  597. {var $1=this.ROUNDS.length;r=0;r:for(;$1>0;$1--,r++){
  598. if (this.roundingMode==this.ROUNDS[r])
  599. {
  600. roundword=this.ROUNDWORDS[r];
  601. break r;
  602. }
  603. }
  604. }/*r*/
  605. return "digits="+this.digits+" "+"form="+formstr+" "+"lostDigits="+(this.lostDigits?"1":"0")+" "+"roundingMode="+roundword;
  606. }
  607. /* <sgml> Test whether round is valid. </sgml> */
  608. // This could be made shared for use by BigDecimal for setScale.
  609. //--private static boolean isValidRound(int testround){
  610. function isValidRound(testround) {
  611. //--int r=0;
  612. var r=0;
  613. {var $2=this.ROUNDS.length;r=0;r:for(;$2>0;$2--,r++){
  614. if (testround==this.ROUNDS[r])
  615. return true;
  616. }
  617. }/*r*/
  618. return false;
  619. }
  620. return MathContext;
  621. })();
  622. var BigDecimal = (function (MathContext) {
  623. /* Generated from 'BigDecimal.nrx' 8 Sep 2000 11:10:50 [v2.00] */
  624. /* Options: Binary Comments Crossref Format Java Logo Strictargs Strictcase Trace2 Verbose3 */
  625. //--package com.ibm.icu.math;
  626. //--import java.math.BigInteger;
  627. //--import com.ibm.icu.impl.Utility;
  628. /* ------------------------------------------------------------------ */
  629. /* BigDecimal -- Decimal arithmetic for Java */
  630. /* ------------------------------------------------------------------ */
  631. /* Copyright IBM Corporation, 1996, 2000. All Rights Reserved. */
  632. /* */
  633. /* The BigDecimal class provides immutable arbitrary-precision */
  634. /* floating point (including integer) decimal numbers. */
  635. /* */
  636. /* As the numbers are decimal, there is an exact correspondence */
  637. /* between an instance of a BigDecimal object and its String */
  638. /* representation; the BigDecimal class provides direct conversions */
  639. /* to and from String and character array objects, and well as */
  640. /* conversions to and from the Java primitive types (which may not */
  641. /* be exact). */
  642. /* ------------------------------------------------------------------ */
  643. /* Notes: */
  644. /* */
  645. /* 1. A BigDecimal object is never changed in value once constructed; */
  646. /* this avoids the need for locking. Note in particular that the */
  647. /* mantissa array may be shared between many BigDecimal objects, */
  648. /* so that once exposed it must not be altered. */
  649. /* */
  650. /* 2. This class looks at MathContext class fields directly (for */
  651. /* performance). It must not and does not change them. */
  652. /* */
  653. /* 3. Exponent checking is delayed until finish(), as we know */
  654. /* intermediate calculations cannot cause 31-bit overflow. */
  655. /* [This assertion depends on MAX_DIGITS in MathContext.] */
  656. /* */
  657. /* 4. Comments for the public API now follow the javadoc conventions. */
  658. /* The NetRexx -comments option is used to pass these comments */
  659. /* through to the generated Java code (with -format, if desired). */
  660. /* */
  661. /* 5. System.arraycopy is faster than explicit loop as follows */
  662. /* Mean length 4: equal */
  663. /* Mean length 8: x2 */
  664. /* Mean length 16: x3 */
  665. /* Mean length 24: x4 */
  666. /* From prior experience, we expect mean length a little below 8, */
  667. /* but arraycopy is still the one to use, in general, until later */
  668. /* measurements suggest otherwise. */
  669. /* */
  670. /* 6. 'DMSRCN' referred to below is the original (1981) IBM S/370 */
  671. /* assembler code implementation of the algorithms below; it is */
  672. /* now called IXXRCN and is available with the OS/390 and VM/ESA */
  673. /* operating systems. */
  674. /* ------------------------------------------------------------------ */
  675. /* Change History: */
  676. /* 1997.09.02 Initial version (derived from netrexx.lang classes) */
  677. /* 1997.09.12 Add lostDigits checking */
  678. /* 1997.10.06 Change mantissa to a byte array */
  679. /* 1997.11.22 Rework power [did not prepare arguments, etc.] */
  680. /* 1997.12.13 multiply did not prepare arguments */
  681. /* 1997.12.14 add did not prepare and align arguments correctly */
  682. /* 1998.05.02 0.07 packaging changes suggested by Sun and Oracle */
  683. /* 1998.05.21 adjust remainder operator finalization */
  684. /* 1998.06.04 rework to pass MathContext to finish() and round() */
  685. /* 1998.06.06 change format to use round(); support rounding modes */
  686. /* 1998.06.25 rename to BigDecimal and begin merge */
  687. /* zero can now have trailing zeros (i.e., exp\=0) */
  688. /* 1998.06.28 new methods: movePointXxxx, scale, toBigInteger */
  689. /* unscaledValue, valueof */
  690. /* 1998.07.01 improve byteaddsub to allow array reuse, etc. */
  691. /* 1998.07.01 make null testing explicit to avoid JIT bug [Win32] */
  692. /* 1998.07.07 scaled division [divide(BigDecimal, int, int)] */
  693. /* 1998.07.08 setScale, faster equals */
  694. /* 1998.07.11 allow 1E6 (no sign) <sigh>; new double/float conversion */
  695. /* 1998.10.12 change package to com.ibm.icu.math */
  696. /* 1998.12.14 power operator no longer rounds RHS [to match ANSI] */
  697. /* add toBigDecimal() and BigDecimal(java.math.BigDecimal) */
  698. /* 1998.12.29 improve byteaddsub by using table lookup */
  699. /* 1999.02.04 lostdigits=0 behaviour rounds instead of digits+1 guard */
  700. /* 1999.02.05 cleaner code for BigDecimal(char[]) */
  701. /* 1999.02.06 add javadoc comments */
  702. /* 1999.02.11 format() changed from 7 to 2 method form */
  703. /* 1999.03.05 null pointer checking is no longer explicit */
  704. /* 1999.03.05 simplify; changes from discussion with J. Bloch: */
  705. /* null no longer permitted for MathContext; drop boolean, */
  706. /* byte, char, float, short constructor, deprecate double */
  707. /* constructor, no blanks in string constructor, add */
  708. /* offset and length version of char[] constructor; */
  709. /* add valueOf(double); drop booleanValue, charValue; */
  710. /* add ...Exact versions of remaining convertors */
  711. /* 1999.03.13 add toBigIntegerExact */
  712. /* 1999.03.13 1.00 release to IBM Centre for Java Technology */
  713. /* 1999.05.27 1.01 correct 0-0.2 bug under scaled arithmetic */
  714. /* 1999.06.29 1.02 constructors should not allow exponent > 9 digits */
  715. /* 1999.07.03 1.03 lost digits should not be checked if digits=0 */
  716. /* 1999.07.06 lost digits Exception message changed */
  717. /* 1999.07.10 1.04 more work on 0-0.2 (scaled arithmetic) */
  718. /* 1999.07.17 improve messages from pow method */
  719. /* 1999.08.08 performance tweaks */
  720. /* 1999.08.15 fastpath in multiply */
  721. /* 1999.11.05 1.05 fix problem in intValueExact [e.g., 5555555555] */
  722. /* 1999.12.22 1.06 remove multiply fastpath, and improve performance */
  723. /* 2000.01.01 copyright update [Y2K has arrived] */
  724. /* 2000.06.18 1.08 no longer deprecate BigDecimal(double) */
  725. /* ------------------------------------------------------------------ */
  726. /* JavaScript conversion (c) 2003 STZ-IDA and PTV AG, Karlsruhe, Germany */
  727. function div(a, b) {
  728. return (a-(a%b))/b;
  729. }
  730. BigDecimal.prototype.div = div;
  731. function arraycopy(src, srcindex, dest, destindex, length) {
  732. var i;
  733. if (destindex > srcindex) {
  734. // in case src and dest are equals, but also doesn't hurt
  735. // if they are different
  736. for (i = length-1; i >= 0; --i) {
  737. dest[i+destindex] = src[i+srcindex];
  738. }
  739. } else {
  740. for (i = 0; i < length; ++i) {
  741. dest[i+destindex] = src[i+srcindex];
  742. }
  743. }
  744. }
  745. BigDecimal.prototype.arraycopy = arraycopy;
  746. function createArrayWithZeros(length) {
  747. var retVal = new Array(length);
  748. var i;
  749. for (i = 0; i < length; ++i) {
  750. retVal[i] = 0;
  751. }
  752. return retVal;
  753. }
  754. BigDecimal.prototype.createArrayWithZeros = createArrayWithZeros;
  755. /**
  756. * The <code>BigDecimal</code> class implements immutable
  757. * arbitrary-precision decimal numbers. The methods of the
  758. * <code>BigDecimal</code> class provide operations for fixed and
  759. * floating point arithmetic, comparison, format conversions, and
  760. * hashing.
  761. * <p>
  762. * As the numbers are decimal, there is an exact correspondence between
  763. * an instance of a <code>BigDecimal</code> object and its
  764. * <code>String</code> representation; the <code>BigDecimal</code> class
  765. * provides direct conversions to and from <code>String</code> and
  766. * character array (<code>char[]</code>) objects, as well as conversions
  767. * to and from the Java primitive types (which may not be exact) and
  768. * <code>BigInteger</code>.
  769. * <p>
  770. * In the descriptions of constructors and methods in this documentation,
  771. * the value of a <code>BigDecimal</code> number object is shown as the
  772. * result of invoking the <code>toString()</code> method on the object.
  773. * The internal representation of a decimal number is neither defined
  774. * nor exposed, and is not permitted to affect the result of any
  775. * operation.
  776. * <p>
  777. * The floating point arithmetic provided by this class is defined by
  778. * the ANSI X3.274-1996 standard, and is also documented at
  779. * <code>http://www2.hursley.ibm.com/decimal</code>
  780. * <br><i>[This URL will change.]</i>
  781. *
  782. * <h3>Operator methods</h3>
  783. * <p>
  784. * Operations on <code>BigDecimal</code> numbers are controlled by a
  785. * {@link MathContext} object, which provides the context (precision and
  786. * other information) for the operation. Methods that can take a
  787. * <code>MathContext</code> parameter implement the standard arithmetic
  788. * operators for <code>BigDecimal</code> objects and are known as
  789. * <i>operator methods</i>. The default settings provided by the
  790. * constant {@link MathContext#DEFAULT} (<code>digits=9,
  791. * form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP</code>)
  792. * perform general-purpose floating point arithmetic to nine digits of
  793. * precision. The <code>MathContext</code> parameter must not be
  794. * <code>null</code>.
  795. * <p>
  796. * Each operator method also has a version provided which does
  797. * not take a <code>MathContext</code> parameter. For this version of
  798. * each method, the context settings used are <code>digits=0,
  799. * form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP</code>;
  800. * these settings perform fixed point arithmetic with unlimited
  801. * precision, as defined for the original BigDecimal class in Java 1.1
  802. * and Java 1.2.
  803. * <p>
  804. * For monadic operators, only the optional <code>MathContext</code>
  805. * parameter is present; the operation acts upon the current object.
  806. * <p>
  807. * For dyadic operators, a <code>BigDecimal</code> parameter is always
  808. * present; it must not be <code>null</code>.
  809. * The operation acts with the current object being the left-hand operand
  810. * and the <code>BigDecimal</code> parameter being the right-hand operand.
  811. * <p>
  812. * For example, adding two <code>BigDecimal</code> objects referred to
  813. * by the names <code>award</code> and <code>extra</code> could be
  814. * written as any of:
  815. * <p><code>
  816. * award.add(extra)
  817. * <br>award.add(extra, MathContext.DEFAULT)
  818. * <br>award.add(extra, acontext)
  819. * </code>
  820. * <p>
  821. * (where <code>acontext</code> is a <code>MathContext</code> object),
  822. * which would return a <code>BigDecimal</code> object whose value is
  823. * the result of adding <code>award</code> and <code>extra</code> under
  824. * the appropriate context settings.
  825. * <p>
  826. * When a <code>BigDecimal</code> operator method is used, a set of
  827. * rules define what the result will be (and, by implication, how the
  828. * result would be represented as a character string).
  829. * These rules are defined in the BigDecimal arithmetic documentation
  830. * (see the URL above), but in summary:
  831. * <ul>
  832. * <li>Results are normally calculated with up to some maximum number of
  833. * significant digits.
  834. * For example, if the <code>MathContext</code> parameter for an operation
  835. * were <code>MathContext.DEFAULT</code> then the result would be
  836. * rounded to 9 digits; the division of 2 by 3 would then result in
  837. * 0.666666667.
  838. * <br>
  839. * You can change the default of 9 significant digits by providing the
  840. * method with a suitable <code>MathContext</code> object. This lets you
  841. * calculate using as many digits as you need -- thousands, if necessary.
  842. * Fixed point (scaled) arithmetic is indicated by using a
  843. * <code>digits</code> setting of 0 (or omitting the
  844. * <code>MathContext</code> parameter).
  845. * <br>
  846. * Similarly, you can change the algorithm used for rounding from the
  847. * default "classic" algorithm.
  848. * <li>
  849. * In standard arithmetic (that is, when the <code>form</code> setting
  850. * is not <code>PLAIN</code>), a zero result is always expressed as the
  851. * single digit <code>'0'</code> (that is, with no sign, decimal point,
  852. * or exponent part).
  853. * <li>
  854. * Except for the division and power operators in standard arithmetic,
  855. * trailing zeros are preserved (this is in contrast to binary floating
  856. * point operations and most electronic calculators, which lose the
  857. * information about trailing zeros in the fractional part of results).
  858. * <br>
  859. * So, for example:
  860. * <p><code>
  861. * new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40"
  862. * <br>new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40"
  863. * <br>new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80"
  864. * <br>new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"
  865. * </code>
  866. * <p>where the value on the right of the <code>=></code> would be the
  867. * result of the operation, expressed as a <code>String</code>, and
  868. * <code>def</code> (in this and following examples) refers to
  869. * <code>MathContext.DEFAULT</code>).
  870. * This preservation of trailing zeros is desirable for most
  871. * calculations (including financial calculations).
  872. * If necessary, trailing zeros may be easily removed using division by 1.
  873. * <li>
  874. * In standard arithmetic, exponential form is used for a result
  875. * depending on its value and the current setting of <code>digits</code>
  876. * (the default is 9 digits).
  877. * If the number of places needed before the decimal point exceeds the
  878. * <code>digits</code> setting, or the absolute value of the number is
  879. * less than <code>0.000001</code>, then the number will be expressed in
  880. * exponential notation; thus
  881. * <p><code>
  882. * new BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)
  883. * </code>
  884. * <p>results in <code>1E+12</code> instead of
  885. * <code>1000000000000</code>, and
  886. * <p><code>
  887. * new BigDecimal("1").divide(new BigDecimal("3E+10"), def)
  888. * </code>
  889. * <p>results in <code>3.33333333E-11</code> instead of
  890. * <code>0.0000000000333333333</code>.
  891. * <p>
  892. * The form of the exponential notation (scientific or engineering) is
  893. * determined by the <code>form</code> setting.
  894. * <eul>
  895. * <p>
  896. * The names of methods in this class follow the conventions established
  897. * by <code>java.lang.Number</code>, <code>java.math.BigInteger</code>,
  898. * and <code>java.math.BigDecimal</code> in Java 1.1 and Java 1.2.
  899. *
  900. * @see MathContext
  901. * @author Mike Cowlishaw
  902. * @stable ICU 2.0
  903. */
  904. //--public class BigDecimal extends java.lang.Number implements java.io.Serializable,java.lang.Comparable{
  905. //-- private static final java.lang.String $0="BigDecimal.nrx";
  906. //-- methods
  907. BigDecimal.prototype.abs = abs;
  908. BigDecimal.prototype.add = add;
  909. BigDecimal.prototype.compareTo = compareTo;
  910. BigDecimal.prototype.divide = divide;
  911. BigDecimal.prototype.divideInteger = divideInteger;
  912. BigDecimal.prototype.max = max;
  913. BigDecimal.prototype.min = min;
  914. BigDecimal.prototype.multiply = multiply;
  915. BigDecimal.prototype.negate = negate;
  916. BigDecimal.prototype.plus = plus;
  917. BigDecimal.prototype.pow = pow;
  918. BigDecimal.prototype.remainder = remainder;
  919. BigDecimal.prototype.subtract = subtract;
  920. BigDecimal.prototype.equals = equals;
  921. BigDecimal.prototype.format = format;
  922. BigDecimal.prototype.intValueExact = intValueExact;
  923. BigDecimal.prototype.movePointLeft = movePointLeft;
  924. BigDecimal.prototype.movePointRight = movePointRight;
  925. BigDecimal.prototype.scale = scale;
  926. BigDecimal.prototype.setScale = setScale;
  927. BigDecimal.prototype.signum = signum;
  928. BigDecimal.prototype.toString = toString;
  929. BigDecimal.prototype.layout = layout;
  930. BigDecimal.prototype.intcheck = intcheck;
  931. BigDecimal.prototype.dodivide = dodivide;
  932. BigDecimal.prototype.bad = bad;
  933. BigDecimal.prototype.badarg = badarg;
  934. BigDecimal.prototype.extend = extend;
  935. BigDecimal.prototype.byteaddsub = byteaddsub;
  936. BigDecimal.prototype.diginit = diginit;
  937. BigDecimal.prototype.clone = clone;
  938. BigDecimal.prototype.checkdigits = checkdigits;
  939. BigDecimal.prototype.round = round;
  940. BigDecimal.prototype.allzero = allzero;
  941. BigDecimal.prototype.finish = finish;
  942. // Convenience methods
  943. BigDecimal.prototype.isGreaterThan = isGreaterThan;
  944. BigDecimal.prototype.isLessThan = isLessThan;
  945. BigDecimal.prototype.isGreaterThanOrEqualTo = isGreaterThanOrEqualTo;
  946. BigDecimal.prototype.isLessThanOrEqualTo = isLessThanOrEqualTo;
  947. BigDecimal.prototype.isPositive = isPositive;
  948. BigDecimal.prototype.isNegative = isNegative;
  949. BigDecimal.prototype.isZero = isZero;
  950. /* ----- Constants ----- */
  951. /* properties constant public */ // useful to others
  952. // the rounding modes (copied here for upwards compatibility)
  953. /**
  954. * Rounding mode to round to a more positive number.
  955. * @see MathContext#ROUND_CEILING
  956. * @stable ICU 2.0
  957. */
  958. //--public static final int ROUND_CEILING=com.ibm.icu.math.MathContext.ROUND_CEILING;
  959. BigDecimal.ROUND_CEILING = BigDecimal.prototype.ROUND_CEILING = MathContext.prototype.ROUND_CEILING;
  960. /**
  961. * Rounding mode to round towards zero.
  962. * @see MathContext#ROUND_DOWN
  963. * @stable ICU 2.0
  964. */
  965. //--public static final int ROUND_DOWN=com.ibm.icu.math.MathContext.ROUND_DOWN;
  966. BigDecimal.ROUND_DOWN = BigDecimal.prototype.ROUND_DOWN = MathContext.prototype.ROUND_DOWN;
  967. /**
  968. * Rounding mode to round to a more negative number.
  969. * @see MathContext#ROUND_FLOOR
  970. * @stable ICU 2.0
  971. */
  972. //--public static final int ROUND_FLOOR=com.ibm.icu.math.MathContext.ROUND_FLOOR;
  973. BigDecimal.ROUND_FLOOR = BigDecimal.prototype.ROUND_FLOOR = MathContext.prototype.ROUND_FLOOR;
  974. /**
  975. * Rounding mode to round to nearest neighbor, where an equidistant
  976. * value is rounded down.
  977. * @see MathContext#ROUND_HALF_DOWN
  978. * @stable ICU 2.0
  979. */
  980. //--public static final int ROUND_HALF_DOWN=com.ibm.icu.math.MathContext.ROUND_HALF_DOWN;
  981. BigDecimal.ROUND_HALF_DOWN = BigDecimal.prototype.ROUND_HALF_DOWN = MathContext.prototype.ROUND_HALF_DOWN;
  982. /**
  983. * Rounding mode to round to nearest neighbor, where an equidistant
  984. * value is rounded to the nearest even neighbor.
  985. * @see MathContext#ROUND_HALF_EVEN
  986. * @stable ICU 2.0
  987. */
  988. //--public static final int ROUND_HALF_EVEN=com.ibm.icu.math.MathContext.ROUND_HALF_EVEN;
  989. BigDecimal.ROUND_HALF_EVEN = BigDecimal.prototype.ROUND_HALF_EVEN = MathContext.prototype.ROUND_HALF_EVEN;
  990. /**
  991. * Rounding mode to round to nearest neighbor, where an equidistant
  992. * value is rounded up.
  993. * @see MathContext#ROUND_HALF_UP
  994. * @stable ICU 2.0
  995. */
  996. //--public static final int ROUND_HALF_UP=com.ibm.icu.math.MathContext.ROUND_HALF_UP;
  997. BigDecimal.ROUND_HALF_UP = BigDecimal.prototype.ROUND_HALF_UP = MathContext.prototype.ROUND_HALF_UP;
  998. /**
  999. * Rounding mode to assert that no rounding is necessary.
  1000. * @see MathContext#ROUND_UNNECESSARY
  1001. * @stable ICU 2.0
  1002. */
  1003. //--public static final int ROUND_UNNECESSARY=com.ibm.icu.math.MathContext.ROUND_UNNECESSARY;
  1004. BigDecimal.ROUND_UNNECESSARY = BigDecimal.prototype.ROUND_UNNECESSARY = MathContext.prototype.ROUND_UNNECESSARY;
  1005. /**
  1006. * Rounding mode to round away from zero.
  1007. * @see MathContext#ROUND_UP
  1008. * @stable ICU 2.0
  1009. */
  1010. //--public static final int ROUND_UP=com.ibm.icu.math.MathContext.ROUND_UP;
  1011. BigDecimal.ROUND_UP = BigDecimal.prototype.ROUND_UP = MathContext.prototype.ROUND_UP;
  1012. /* properties constant private */ // locals
  1013. //--private static final byte ispos=1; // ind: indicates positive (must be 1)
  1014. //--private static final byte iszero=0; // ind: indicates zero (must be 0)
  1015. //--private static final byte isneg=-1; // ind: indicates negative (must be -1)
  1016. BigDecimal.prototype.ispos = 1;
  1017. BigDecimal.prototype.iszero = 0;
  1018. BigDecimal.prototype.isneg = -1;
  1019. // [later could add NaN, +/- infinity, here]
  1020. //--private static final int MinExp=-999999999; // minimum exponent allowed
  1021. //--private static final int MaxExp=999999999; // maximum exponent allowed
  1022. //--private static final int MinArg=-999999999; // minimum argument integer
  1023. //--private static final int MaxArg=999999999; // maximum argument integer
  1024. BigDecimal.prototype.MinExp=-999999999; // minimum exponent allowed
  1025. BigDecimal.prototype.MaxExp=999999999; // maximum exponent allowed
  1026. BigDecimal.prototype.MinArg=-999999999; // minimum argument integer
  1027. BigDecimal.prototype.MaxArg=999999999; // maximum argument integer
  1028. //--private static final com.ibm.icu.math.MathContext plainMC=new com.ibm.icu.math.MathContext(0,com.ibm.icu.math.MathContext.PLAIN); // context for plain unlimited math
  1029. BigDecimal.prototype.plainMC=new MathContext(0, MathContext.prototype.PLAIN);
  1030. /* properties constant private unused */ // present but not referenced
  1031. // Serialization version
  1032. //--private static final long serialVersionUID=8245355804974198832L;
  1033. //--private static final java.lang.String copyright=" Copyright (c) IBM Corporation 1996, 2000. All rights reserved. ";
  1034. /* properties static private */
  1035. // Precalculated constant arrays (used by byteaddsub)
  1036. //--private static byte bytecar[]=new byte[(90+99)+1]; // carry/borrow array
  1037. //--private static byte bytedig[]=diginit(); // next digit array
  1038. BigDecimal.prototype.bytecar = new Array((90+99)+1);
  1039. BigDecimal.prototype.bytedig = diginit();
  1040. /**
  1041. * The <code>BigDecimal</code> constant "0".
  1042. *
  1043. * @see #ONE
  1044. * @see #TEN
  1045. * @stable ICU 2.0
  1046. */
  1047. //--public static final com.ibm.icu.math.BigDecimal ZERO=new com.ibm.icu.math.BigDecimal((long)0); // use long as we want the int constructor
  1048. // .. to be able to use this, for speed
  1049. BigDecimal.ZERO = BigDecimal.prototype.ZERO = new BigDecimal("0");
  1050. /**
  1051. * The <code>BigDecimal</code> constant "1".
  1052. *
  1053. * @see #TEN
  1054. * @see #ZERO
  1055. * @stable ICU 2.0
  1056. */
  1057. //--public static final com.ibm.icu.math.BigDecimal ONE=new com.ibm.icu.math.BigDecimal((long)1); // use long as we want the int constructor
  1058. // .. to be able to use this, for speed
  1059. BigDecimal.ONE = BigDecimal.prototype.ONE = new BigDecimal("1");
  1060. /**
  1061. * The <code>BigDecimal</code> constant "10".
  1062. *
  1063. * @see #ONE
  1064. * @see #ZERO
  1065. * @stable ICU 2.0
  1066. */
  1067. //--public static final com.ibm.icu.math.BigDecimal TEN=new com.ibm.icu.math.BigDecimal(10);
  1068. BigDecimal.TEN = BigDecimal.prototype.TEN = new BigDecimal("10");
  1069. /* ----- Instance properties [all private and immutable] ----- */
  1070. /* properties private */
  1071. /**
  1072. * The indicator. This may take the values:
  1073. * <ul>
  1074. * <li>ispos -- the number is positive
  1075. * <li>iszero -- the number is zero
  1076. * <li>isneg -- the number is negative
  1077. * </ul>
  1078. *
  1079. * @serial
  1080. */
  1081. //--private byte ind; // assumed undefined
  1082. // Note: some code below assumes IND = Sign [-1, 0, 1], at present.
  1083. // We only need two bits for this, but use a byte [also permits
  1084. // smooth future extension].
  1085. /**
  1086. * The formatting style. This may take the values:
  1087. * <ul>
  1088. * <li>MathContext.PLAIN -- no exponent needed
  1089. * <li>MathContext.SCIENTIFIC -- scientific notation required
  1090. * <li>MathContext.ENGINEERING -- engineering notation required
  1091. * </ul>
  1092. * <p>
  1093. * This property is an optimization; it allows us to defer number
  1094. * layout until it is actually needed as a string, hence avoiding
  1095. * unnecessary formatting.
  1096. *
  1097. * @serial
  1098. */
  1099. //--private byte form=(byte)com.ibm.icu.math.MathContext.PLAIN; // assumed PLAIN
  1100. // We only need two bits for this, at present, but use a byte
  1101. // [again, to allow for smooth future extension]
  1102. /**
  1103. * The value of the mantissa.
  1104. * <p>
  1105. * Once constructed, this may become shared between several BigDecimal
  1106. * objects, so must not be altered.
  1107. * <p>
  1108. * For efficiency (speed), this is a byte array, with each byte
  1109. * taking a value of 0 -> 9.
  1110. * <p>
  1111. * If the first byte is 0 then the value of the number is zero (and
  1112. * mant.length=1, except when constructed from a plain number, for
  1113. * example, 0.000).
  1114. *
  1115. * @serial
  1116. */
  1117. //--private byte mant[]; // assumed null
  1118. /**
  1119. * The exponent.
  1120. * <p>
  1121. * For fixed point arithmetic, scale is <code>-exp</code>, and can
  1122. * apply to zero.
  1123. *
  1124. * Note that this property can have a value less than MinExp when
  1125. * the mantissa has more than one digit.
  1126. *
  1127. * @serial
  1128. */
  1129. //--private int exp;
  1130. // assumed 0
  1131. /* ---------------------------------------------------------------- */
  1132. /* Constructors */
  1133. /* ---------------------------------------------------------------- */
  1134. /**
  1135. * Constructs a <code>BigDecimal</code> object from a
  1136. * <code>java.math.BigDecimal</code>.
  1137. * <p>
  1138. * Constructs a <code>BigDecimal</code> as though the parameter had
  1139. * been represented as a <code>String</code> (using its
  1140. * <code>toString</code> method) and the
  1141. * {@link #BigDecimal(java.lang.String)} constructor had then been
  1142. * used.
  1143. * The parameter must not be <code>null</code>.
  1144. * <p>
  1145. * <i>(Note: this constructor is provided only in the
  1146. * <code>com.ibm.icu.math</code> version of the BigDecimal class.
  1147. * It would not be present in a <code>java.math</code> version.)</i>
  1148. *
  1149. * @param bd The <code>BigDecimal</code> to be translated.
  1150. * @stable ICU 2.0
  1151. */
  1152. //--public BigDecimal(java.math.BigDecimal bd){
  1153. //-- this(bd.toString());
  1154. //-- return;}
  1155. /**
  1156. * Constructs a <code>BigDecimal</code> object from a
  1157. * <code>BigInteger</code>, with scale 0.
  1158. * <p>
  1159. * Constructs a <code>BigDecimal</code> which is the exact decimal
  1160. * representation of the <code>BigInteger</code>, with a scale of
  1161. * zero.
  1162. * The value of the <code>BigDecimal</code> is identical to the value
  1163. * of the <code>BigInteger</code>.
  1164. * The parameter must not be <code>null</code>.
  1165. * <p>
  1166. * The <code>BigDecimal</code> will contain only decimal digits,
  1167. * prefixed with a leading minus sign (hyphen) if the
  1168. * <code>BigInteger</code> is negative. A leading zero will be
  1169. * present only if the <code>BigInteger</code> is zero.
  1170. *
  1171. * @param bi The <code>BigInteger</code> to be converted.
  1172. * @stable ICU 2.0
  1173. */
  1174. //--public BigDecimal(java.math.BigInteger bi){
  1175. //-- this(bi.toString(10));
  1176. //-- return;}
  1177. // exp remains 0
  1178. /**
  1179. * Constructs a <code>BigDecimal</code> object from a
  1180. * <code>BigInteger</code> and a scale.
  1181. * <p>
  1182. * Constructs a <code>BigDecimal</code> which is the exact decimal
  1183. * representation of the <code>BigInteger</code>, scaled by the
  1184. * second parameter, which may not be negative.
  1185. * The value of the <code>BigDecimal</code> is the
  1186. * <code>BigInteger</code> divided by ten to the power of the scale.
  1187. * The <code>BigInteger</code> parameter must not be
  1188. * <code>null</code>.
  1189. * <p>
  1190. * The <code>BigDecimal</code> will contain only decimal digits, (with
  1191. * an embedded decimal point followed by <code>scale</code> decimal
  1192. * digits if the scale is positive), prefixed with a leading minus
  1193. * sign (hyphen) if the <code>BigInteger</code> is negative. A
  1194. * leading zero will be present only if the <code>BigInteger</code> is
  1195. * zero.
  1196. *
  1197. * @param bi The <code>BigInteger</code> to be converted.
  1198. * @param scale The <code>int</code> specifying the scale.
  1199. * @throws NumberFormatException if the scale is negative.
  1200. * @stable ICU 2.0
  1201. */
  1202. //--public BigDecimal(java.math.BigInteger bi,int scale){
  1203. //-- this(bi.toString(10));
  1204. //-- if (scale<0)
  1205. //-- throw new java.lang.NumberFormatException("Negative scale:"+" "+scale);
  1206. //-- exp=(int)-scale; // exponent is -scale
  1207. //-- return;}
  1208. /**
  1209. * Constructs a <code>BigDecimal</code> object from an array of characters.
  1210. * <p>
  1211. * Constructs a <code>BigDecimal</code> as though a
  1212. * <code>String</code> had been constructed from the character array
  1213. * and the {@link #BigDecimal(java.lang.String)} constructor had then
  1214. * been used. The parameter must not be <code>null</code>.
  1215. * <p>
  1216. * Using this constructor is faster than using the
  1217. * <code>BigDecimal(String)</code> constructor if the string is
  1218. * already available in character array form.
  1219. *
  1220. * @param inchars The <code>char[]</code> array containing the number
  1221. * to be converted.
  1222. * @throws NumberFormatException if the parameter is not a valid
  1223. * number.
  1224. * @stable ICU 2.0
  1225. */
  1226. //--public BigDecimal(char inchars[]){
  1227. //-- this(inchars,0,inchars.length);
  1228. //-- return;}
  1229. /**
  1230. * Constructs a <code>BigDecimal</code> object from an array of characters.
  1231. * <p>
  1232. * Constructs a <code>BigDecimal</code> as though a
  1233. * <code>String</code> had been constructed from the character array
  1234. * (or a subarray of that array) and the
  1235. * {@link #BigDecimal(java.lang.String)} constructor had then been
  1236. * used. The first parameter must not be <code>null</code>, and the
  1237. * subarray must be wholly contained within it.
  1238. * <p>
  1239. * Using this constructor is faster than using the
  1240. * <code>BigDecimal(String)</code> constructor if the string is
  1241. * already available within a character array.
  1242. *
  1243. * @param inchars The <code>char[]</code> array containing the number
  1244. * to be converted.
  1245. * @param offset The <code>int</code> offset into the array of the
  1246. * start of the number to be converted.
  1247. * @param length The <code>int</code> length of the number.
  1248. * @throws NumberFormatException if the parameter is not a valid
  1249. * number for any reason.
  1250. * @stable ICU 2.0
  1251. */
  1252. //--public BigDecimal(char inchars[],int offset,int length){super();
  1253. function BigDecimal() {
  1254. //-- members
  1255. this.ind = 0;
  1256. this.form = MathContext.prototype.PLAIN;
  1257. this.mant = null;
  1258. this.exp = 0;
  1259. //-- overloaded ctor
  1260. if (BigDecimal.arguments.length == 0)
  1261. return;
  1262. var inchars;
  1263. var offset;
  1264. var length;
  1265. if (BigDecimal.arguments.length == 1)
  1266. {
  1267. inchars = BigDecimal.arguments[0];
  1268. offset = 0;
  1269. length = inchars.length;
  1270. }
  1271. else
  1272. {
  1273. inchars = BigDecimal.arguments[0];
  1274. offset = BigDecimal.arguments[1];
  1275. length = BigDecimal.arguments[2];
  1276. }
  1277. if (typeof inchars == "string")
  1278. {
  1279. inchars = inchars.split("");
  1280. }
  1281. //--boolean exotic;
  1282. var exotic;
  1283. //--boolean hadexp;
  1284. var hadexp;
  1285. //--int d;
  1286. var d;
  1287. //--int dotoff;
  1288. var dotoff;
  1289. //--int last;
  1290. var last;
  1291. //--int i=0;
  1292. var i=0;
  1293. //--char si=0;
  1294. var si=0;
  1295. //--boolean eneg=false;
  1296. var eneg=false;
  1297. //--int k=0;
  1298. var k=0;
  1299. //--int elen=0;
  1300. var elen=0;
  1301. //--int j=0;
  1302. var j=0;
  1303. //--char sj=0;
  1304. var sj=0;
  1305. //--int dvalue=0;
  1306. var dvalue=0;
  1307. //--int mag=0;
  1308. var mag=0;
  1309. // This is the primary constructor; all incoming strings end up
  1310. // here; it uses explicit (inline) parsing for speed and to avoid
  1311. // generating intermediate (temporary) objects of any kind.
  1312. // 1998.06.25: exponent form built only if E/e in string
  1313. // 1998.06.25: trailing zeros not removed for zero
  1314. // 1999.03.06: no embedded blanks; allow offset and length
  1315. if (length<=0)
  1316. this.bad("BigDecimal(): ", inchars); // bad conversion (empty string)
  1317. // [bad offset will raise array bounds exception]
  1318. /* Handle and step past sign */
  1319. this.ind=this.ispos; // assume positive
  1320. if (inchars[0]==('-'))
  1321. {
  1322. length--;
  1323. if (length==0)
  1324. this.bad("BigDecimal(): ", inchars); // nothing after sign
  1325. this.ind=this.isneg;
  1326. offset++;
  1327. }
  1328. else
  1329. if (inchars[0]==('+'))
  1330. {
  1331. length--;
  1332. if (length==0)
  1333. this.bad("BigDecimal(): ", inchars); // nothing after sign
  1334. offset++;
  1335. }
  1336. /* We're at the start of the number */
  1337. exotic=false; // have extra digits
  1338. hadexp=false; // had explicit exponent
  1339. d=0; // count of digits found
  1340. dotoff=-1; // offset where dot was found
  1341. last=-1; // last character of mantissa
  1342. {var $1=length;i=offset;i:for(;$1>0;$1--,i++){
  1343. si=inchars[i];
  1344. if (si>='0') // test for Arabic digit
  1345. if (si<='9')
  1346. {
  1347. last=i;
  1348. d++; // still in mantissa
  1349. continue i;
  1350. }
  1351. if (si=='.')
  1352. { // record and ignore
  1353. if (dotoff>=0)
  1354. this.bad("BigDecimal(): ", inchars); // two dots
  1355. dotoff=i-offset; // offset into mantissa
  1356. continue i;
  1357. }
  1358. if (si!='e')
  1359. if (si!='E')
  1360. { // expect an extra digit
  1361. if (si<'0' || si>'9')
  1362. this.bad("BigDecimal(): ", inchars); // not a number
  1363. // defer the base 10 check until later to avoid extra method call
  1364. exotic=true; // will need conversion later
  1365. last=i;
  1366. d++; // still in mantissa
  1367. continue i;
  1368. }
  1369. /* Found 'e' or 'E' -- now process explicit exponent */
  1370. // 1998.07.11: sign no longer required
  1371. if ((i-offset)>(length-2))
  1372. this.bad("BigDecimal(): ", inchars); // no room for even one digit
  1373. eneg=false;
  1374. if ((inchars[i+1])==('-'))
  1375. {
  1376. eneg=true;
  1377. k=i+2;
  1378. }
  1379. else
  1380. if ((inchars[i+1])==('+'))
  1381. k=i+2;
  1382. else
  1383. k=i+1;
  1384. // k is offset of first expected digit
  1385. elen=length-((k-offset)); // possible number of digits
  1386. if ((elen==0)||(elen>9))
  1387. this.bad("BigDecimal(): ", inchars); // 0 or more than 9 digits
  1388. {var $2=elen;j=k;j:for(;$2>0;$2--,j++){
  1389. sj=inchars[j];
  1390. if (sj<'0')
  1391. this.bad("BigDecimal(): ", inchars); // always bad
  1392. if (sj>'9')
  1393. { // maybe an exotic digit
  1394. /*if (si<'0' || si>'9')
  1395. this.bad(inchars); // not a number
  1396. dvalue=java.lang.Character.digit(sj,10); // check base
  1397. if (dvalue<0)
  1398. bad(inchars); // not base 10*/
  1399. this.bad("BigDecimal(): ", inchars);
  1400. }
  1401. else
  1402. dvalue=sj-'0';
  1403. this.exp=(this.exp*10)+dvalue;
  1404. }
  1405. }/*j*/
  1406. if (eneg)
  1407. this.exp=-this.exp; // was negative
  1408. hadexp=true; // remember we had one
  1409. break i; // we are done
  1410. }
  1411. }/*i*/
  1412. /* Here when all inspected */
  1413. if (d==0)
  1414. this.bad("BigDecimal(): ", inchars); // no mantissa digits
  1415. if (dotoff>=0)
  1416. this.exp=(this.exp+dotoff)-d; // adjust exponent if had dot
  1417. /* strip leading zeros/dot (leave final if all 0's) */
  1418. {var $3=last-1;i=offset;i:for(;i<=$3;i++){
  1419. si=inchars[i];
  1420. if (si=='0')
  1421. {
  1422. offset++;
  1423. dotoff--;
  1424. d--;
  1425. }
  1426. else
  1427. if (si=='.')
  1428. {
  1429. offset++; // step past dot
  1430. dotoff--;
  1431. }
  1432. else
  1433. if (si<='9')
  1434. break i;/* non-0 */
  1435. else
  1436. {/* exotic */
  1437. //if ((java.lang.Character.digit(si,10))!=0)
  1438. break i; // non-0 or bad
  1439. // is 0 .. strip like '0'
  1440. //offset++;
  1441. //dotoff--;
  1442. //d--;
  1443. }
  1444. }
  1445. }/*i*/
  1446. /* Create the mantissa array */
  1447. this.mant=new Array(d); // we know the length
  1448. j=offset; // input offset
  1449. if (exotic)
  1450. {exotica:do{ // slow: check for exotica
  1451. {var $4=d;i=0;i:for(;$4>0;$4--,i++){
  1452. if (i==dotoff)
  1453. j++; // at dot
  1454. sj=inchars[j];
  1455. if (sj<='9')
  1456. this.mant[i]=sj-'0';/* easy */
  1457. else
  1458. {
  1459. //dvalue=java.lang.Character.digit(sj,10);
  1460. //if (dvalue<0)
  1461. this.bad("BigDecimal(): ", inchars); // not a number after all
  1462. //mant[i]=(byte)dvalue;
  1463. }
  1464. j++;
  1465. }
  1466. }/*i*/
  1467. }while(false);}/*exotica*/
  1468. else
  1469. {simple:do{
  1470. {var $5=d;i=0;i:for(;$5>0;$5--,i++){
  1471. if (i==dotoff)
  1472. j++;
  1473. this.mant[i]=inchars[j]-'0';
  1474. j++;
  1475. }
  1476. }/*i*/
  1477. }while(false);}/*simple*/
  1478. /* Looks good. Set the sign indicator and form, as needed. */
  1479. // Trailing zeros are preserved
  1480. // The rule here for form is:
  1481. // If no E-notation, then request plain notation
  1482. // Otherwise act as though add(0,DEFAULT) and request scientific notation
  1483. // [form is already PLAIN]
  1484. if (this.mant[0]==0)
  1485. {
  1486. this.ind=this.iszero; // force to show zero
  1487. // negative exponent is significant (e.g., -3 for 0.000) if plain
  1488. if (this.exp>0)
  1489. this.exp=0; // positive exponent can be ignored
  1490. if (hadexp)
  1491. { // zero becomes single digit from add
  1492. this.mant=this.ZERO.mant;
  1493. this.exp=0;
  1494. }
  1495. }
  1496. else
  1497. { // non-zero
  1498. // [ind was set earlier]
  1499. // now determine form
  1500. if (hadexp)
  1501. {
  1502. this.form=MathContext.prototype.SCIENTIFIC;
  1503. // 1999.06.29 check for overflow
  1504. mag=(this.exp+this.mant.length)-1; // true exponent in scientific notation
  1505. if ((mag<this.MinExp)||(mag>this.MaxExp))
  1506. this.bad("BigDecimal(): ", inchars);
  1507. }
  1508. }
  1509. // say 'BD(c[]): mant[0] mantlen exp ind form:' mant[0] mant.length exp ind form
  1510. return;
  1511. }
  1512. /**
  1513. * Constructs a <code>BigDecimal</code> object directly from a
  1514. * <code>double</code>.
  1515. * <p>
  1516. * Constructs a <code>BigDecimal</code> which is the exact decimal
  1517. * representation of the 64-bit signed binary floating point
  1518. * parameter.
  1519. * <p>
  1520. * Note that this constructor it an exact conversion; it does not give
  1521. * the same result as converting <code>num</code> to a
  1522. * <code>String</code> using the <code>Double.toString()</code> method
  1523. * and then using the {@link #BigDecimal(java.lang.String)}
  1524. * constructor.
  1525. * To get that result, use the static {@link #valueOf(double)}
  1526. * method to construct a <code>BigDecimal</code> from a
  1527. * <code>double</code>.
  1528. *
  1529. * @param num The <code>double</code> to be converted.
  1530. * @throws NumberFormatException if the parameter is infinite or
  1531. * not a number.
  1532. * @stable ICU 2.0
  1533. */
  1534. //--public BigDecimal(double num){
  1535. //-- // 1999.03.06: use exactly the old algorithm
  1536. //-- // 2000.01.01: note that this constructor does give an exact result,
  1537. //-- // so perhaps it should not be deprecated
  1538. //-- // 2000.06.18: no longer deprecated
  1539. //-- this((new java.math.BigDecimal(num)).toString());
  1540. //-- return;}
  1541. /**
  1542. * Constructs a <code>BigDecimal</code> object directly from a
  1543. * <code>int</code>.
  1544. * <p>
  1545. * Constructs a <code>BigDecimal</code> which is the exact decimal
  1546. * representation of the 32-bit signed binary integer parameter.
  1547. * The <code>BigDecimal</code> will contain only decimal digits,
  1548. * prefixed with a leading minus sign (hyphen) if the parameter is
  1549. * negative.
  1550. * A leading zero will be present only if the parameter is zero.
  1551. *
  1552. * @param num The <code>int</code> to be converted.
  1553. * @stable ICU 2.0
  1554. */
  1555. //--public BigDecimal(int num){super();
  1556. //-- int mun;
  1557. //-- int i=0;
  1558. //-- // We fastpath commoners
  1559. //-- if (num<=9)
  1560. //-- if (num>=(-9))
  1561. //-- {singledigit:do{
  1562. //-- // very common single digit case
  1563. //-- {/*select*/
  1564. //-- if (num==0)
  1565. //-- {
  1566. //-- mant=ZERO.mant;
  1567. //-- ind=iszero;
  1568. //-- }
  1569. //-- else if (num==1)
  1570. //-- {
  1571. //-- mant=ONE.mant;
  1572. //-- ind=ispos;
  1573. //-- }
  1574. //-- else if (num==(-1))
  1575. //-- {
  1576. //-- mant=ONE.mant;
  1577. //-- ind=isneg;
  1578. //-- }
  1579. //-- else{
  1580. //-- {
  1581. //-- mant=new byte[1];
  1582. //-- if (num>0)
  1583. //-- {
  1584. //-- mant[0]=(byte)num;
  1585. //-- ind=ispos;
  1586. //-- }
  1587. //-- else
  1588. //-- { // num<-1
  1589. //-- mant[0]=(byte)((int)-num);
  1590. //-- ind=isneg;
  1591. //-- }
  1592. //-- }
  1593. //-- }
  1594. //-- }
  1595. //-- return;
  1596. //-- }while(false);}/*singledigit*/
  1597. //--
  1598. //-- /* We work on negative numbers so we handle the most negative number */
  1599. //-- if (num>0)
  1600. //-- {
  1601. //-- ind=ispos;
  1602. //-- num=(int)-num;
  1603. //-- }
  1604. //-- else
  1605. //-- ind=isneg;/* negative */ // [0 case already handled]
  1606. //-- // [it is quicker, here, to pre-calculate the length with
  1607. //-- // one loop, then allocate exactly the right length of byte array,
  1608. //-- // then re-fill it with another loop]
  1609. //-- mun=num; // working copy
  1610. //-- {i=9;i:for(;;i--){
  1611. //-- mun=mun/10;
  1612. //-- if (mun==0)
  1613. //-- break i;
  1614. //-- }
  1615. //-- }/*i*/
  1616. //-- // i is the position of the leftmost digit placed
  1617. //-- mant=new byte[10-i];
  1618. //-- {i=(10-i)-1;i:for(;;i--){
  1619. //-- mant[i]=(byte)-(((byte)(num%10)));
  1620. //-- num=num/10;
  1621. //-- if (num==0)
  1622. //-- break i;
  1623. //-- }
  1624. //-- }/*i*/
  1625. //-- return;
  1626. //-- }
  1627. /**
  1628. * Constructs a <code>BigDecimal</code> object directly from a
  1629. * <code>long</code>.
  1630. * <p>
  1631. * Constructs a <code>BigDecimal</code> which is the exact decimal
  1632. * representation of the 64-bit signed binary integer parameter.
  1633. * The <code>BigDecimal</code> will contain only decimal digits,
  1634. * prefixed with a leading minus sign (hyphen) if the parameter is
  1635. * negative.
  1636. * A leading zero will be present only if the parameter is zero.
  1637. *
  1638. * @param num The <code>long</code> to be converted.
  1639. * @stable ICU 2.0
  1640. */
  1641. //--public BigDecimal(long num){super();
  1642. //-- long mun;
  1643. //-- int i=0;
  1644. //-- // Not really worth fastpathing commoners in this constructor [also,
  1645. //-- // we use this to construct the static constants].
  1646. //-- // This is much faster than: this(String.valueOf(num).toCharArray())
  1647. //-- /* We work on negative num so we handle the most negative number */
  1648. //-- if (num>0)
  1649. //-- {
  1650. //-- ind=ispos;
  1651. //-- num=(long)-num;
  1652. //-- }
  1653. //-- else
  1654. //-- if (num==0)
  1655. //-- ind=iszero;
  1656. //-- else
  1657. //-- ind=isneg;/* negative */
  1658. //-- mun=num;
  1659. //-- {i=18;i:for(;;i--){
  1660. //-- mun=mun/10;
  1661. //-- if (mun==0)
  1662. //-- break i;
  1663. //-- }
  1664. //-- }/*i*/
  1665. //-- // i is the position of the leftmost digit placed
  1666. //-- mant=new byte[19-i];
  1667. //-- {i=(19-i)-1;i:for(;;i--){
  1668. //-- mant[i]=(byte)-(((byte)(num%10)));
  1669. //-- num=num/10;
  1670. //-- if (num==0)
  1671. //-- break i;
  1672. //-- }
  1673. //-- }/*i*/
  1674. //-- return;
  1675. //-- }
  1676. /**
  1677. * Constructs a <code>BigDecimal</code> object from a <code>String</code>.
  1678. * <p>
  1679. * Constructs a <code>BigDecimal</code> from the parameter, which must
  1680. * not be <code>null</code> and must represent a valid <i>number</i>,
  1681. * as described formally in the documentation referred to
  1682. * {@link BigDecimal above}.
  1683. * <p>
  1684. * In summary, numbers in <code>String</code> form must have at least
  1685. * one digit, may have a leading sign, may have a decimal point, and
  1686. * exponential notation may be used. They follow conventional syntax,
  1687. * and may not contain blanks.
  1688. * <p>
  1689. * Some valid strings from which a <code>BigDecimal</code> might
  1690. * be constructed are:
  1691. * <pre>
  1692. * "0" -- Zero
  1693. * "12" -- A whole number
  1694. * "-76" -- A signed whole number
  1695. * "12.70" -- Some decimal places
  1696. * "+0.003" -- Plus sign is allowed
  1697. * "17." -- The same as 17
  1698. * ".5" -- The same as 0.5
  1699. * "4E+9" -- Exponential notation
  1700. * "0.73e-7" -- Exponential notation
  1701. * </pre>
  1702. * <p>
  1703. * (Exponential notation means that the number includes an optional
  1704. * sign and a power of ten following an '</code>E</code>' that
  1705. * indicates how the decimal point will be shifted. Thus the
  1706. * <code>"4E+9"</code> above is just a short way of writing
  1707. * <code>4000000000</code>, and the <code>"0.73e-7"</code> is short
  1708. * for <code>0.000000073</code>.)
  1709. * <p>
  1710. * The <code>BigDecimal</code> constructed from the String is in a
  1711. * standard form, with no blanks, as though the
  1712. * {@link #add(BigDecimal)} method had been used to add zero to the
  1713. * number with unlimited precision.
  1714. * If the string uses exponential notation (that is, includes an
  1715. * <code>e</code> or an <code>E</code>), then the
  1716. * <code>BigDecimal</code> number will be expressed in scientific
  1717. * notation (where the power of ten is adjusted so there is a single
  1718. * non-zero digit to the left of the decimal point); in this case if
  1719. * the number is zero then it will be expressed as the single digit 0,
  1720. * and if non-zero it will have an exponent unless that exponent would
  1721. * be 0. The exponent must fit in nine digits both before and after it
  1722. * is expressed in scientific notation.
  1723. * <p>
  1724. * Any digits in the parameter must be decimal; that is,
  1725. * <code>Character.digit(c, 10)</code> (where </code>c</code> is the
  1726. * character in question) would not return -1.
  1727. *
  1728. * @param string The <code>String</code> to be converted.
  1729. * @throws NumberFormatException if the parameter is not a valid
  1730. * number.
  1731. * @stable ICU 2.0
  1732. */
  1733. //--public BigDecimal(java.lang.String string){
  1734. //-- this(string.toCharArray(),0,string.length());
  1735. //-- return;}
  1736. /* <sgml> Make a default BigDecimal object for local use. </sgml> */
  1737. //--private BigDecimal(){super();
  1738. //-- return;
  1739. //-- }
  1740. /* ---------------------------------------------------------------- */
  1741. /* Operator methods [methods which take a context parameter] */
  1742. /* ---------------------------------------------------------------- */
  1743. /**
  1744. * Returns a plain <code>BigDecimal</code> whose value is the absolute
  1745. * value of this <code>BigDecimal</code>.
  1746. * <p>
  1747. * The same as {@link #abs(MathContext)}, where the context is
  1748. * <code>new MathContext(0, MathContext.PLAIN)</code>.
  1749. * <p>
  1750. * The length of the decimal part (the scale) of the result will
  1751. * be <code>this.scale()</code>
  1752. *
  1753. * @return A <code>BigDecimal</code> whose value is the absolute
  1754. * value of this <code>BigDecimal</code>.
  1755. * @stable ICU 2.0
  1756. */
  1757. //--public com.ibm.icu.math.BigDecimal abs(){
  1758. //- return this.abs(plainMC);
  1759. //- }
  1760. /**
  1761. * Returns a <code>BigDecimal</code> whose value is the absolute value
  1762. * of this <code>BigDecimal</code>.
  1763. * <p>
  1764. * If the current object is zero or positive, then the same result as
  1765. * invoking the {@link #plus(MathContext)} method with the same
  1766. * parameter is returned.
  1767. * Otherwise, the same result as invoking the
  1768. * {@link #negate(MathContext)} method with the same parameter is
  1769. * returned.
  1770. *
  1771. * @param set The <code>MathContext</code> arithmetic settings.
  1772. * @return A <code>BigDecimal</code> whose value is the absolute
  1773. * value of this <code>BigDecimal</code>.
  1774. * @stable ICU 2.0
  1775. */
  1776. //--public com.ibm.icu.math.BigDecimal abs(com.ibm.icu.math.MathContext set){
  1777. function abs() {
  1778. var set;
  1779. if (abs.arguments.length == 1)
  1780. {
  1781. set = abs.arguments[0];
  1782. }
  1783. else if (abs.arguments.length == 0)
  1784. {
  1785. set = this.plainMC;
  1786. }
  1787. else
  1788. {
  1789. throw "abs(): " + abs.arguments.length + " arguments given; expected 0 or 1";
  1790. }
  1791. if (this.ind==this.isneg)
  1792. return this.negate(set);
  1793. return this.plus(set);
  1794. }
  1795. /**
  1796. * Returns a plain <code>BigDecimal</code> whose value is
  1797. * <code>this+rhs</code>, using fixed point arithmetic.
  1798. * <p>
  1799. * The same as {@link #add(BigDecimal, MathContext)},
  1800. * where the <code>BigDecimal</code> is <code>rhs</code>,
  1801. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  1802. * <p>
  1803. * The length of the decimal part (the scale) of the result will be
  1804. * the maximum of the scales of the two operands.
  1805. *
  1806. * @param rhs The <code>BigDecimal</code> for the right hand side of
  1807. * the addition.
  1808. * @return A <code>BigDecimal</code> whose value is
  1809. * <code>this+rhs</code>, using fixed point arithmetic.
  1810. * @stable ICU 2.0
  1811. */
  1812. //--public com.ibm.icu.math.BigDecimal add(com.ibm.icu.math.BigDecimal rhs){
  1813. //-- return this.add(rhs,plainMC);
  1814. //-- }
  1815. /**
  1816. * Returns a <code>BigDecimal</code> whose value is <code>this+rhs</code>.
  1817. * <p>
  1818. * Implements the addition (<b><code>+</code></b>) operator
  1819. * (as defined in the decimal documentation, see {@link BigDecimal
  1820. * class header}),
  1821. * and returns the result as a <code>BigDecimal</code> object.
  1822. *
  1823. * @param rhs The <code>BigDecimal</code> for the right hand side of
  1824. * the addition.
  1825. * @param set The <code>MathContext</code> arithmetic settings.
  1826. * @return A <code>BigDecimal</code> whose value is
  1827. * <code>this+rhs</code>.
  1828. * @stable ICU 2.0
  1829. */
  1830. //--public com.ibm.icu.math.BigDecimal add(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  1831. function add() {
  1832. var set;
  1833. if (add.arguments.length == 2)
  1834. {
  1835. set = add.arguments[1];
  1836. }
  1837. else if (add.arguments.length == 1)
  1838. {
  1839. set = this.plainMC;
  1840. }
  1841. else
  1842. {
  1843. throw "add(): " + add.arguments.length + " arguments given; expected 1 or 2";
  1844. }
  1845. var rhs = add.arguments[0];
  1846. //--com.ibm.icu.math.BigDecimal lhs;
  1847. var lhs;
  1848. //--int reqdig;
  1849. var reqdig;
  1850. //--com.ibm.icu.math.BigDecimal res;
  1851. var res;
  1852. //--byte usel[];
  1853. var usel;
  1854. //--int usellen;
  1855. var usellen;
  1856. //--byte user[];
  1857. var user;
  1858. //--int userlen;
  1859. var userlen;
  1860. //--int newlen=0;
  1861. var newlen=0;
  1862. //--int tlen=0;
  1863. var tlen=0;
  1864. //--int mult=0;
  1865. var mult=0;
  1866. //--byte t[]=null;
  1867. var t=null;
  1868. //--int ia=0;
  1869. var ia=0;
  1870. //--int ib=0;
  1871. var ib=0;
  1872. //--int ea=0;
  1873. var ea=0;
  1874. //int eb=0;
  1875. var eb=0;
  1876. //byte ca=0;
  1877. var ca=0;
  1878. //--byte cb=0;
  1879. var cb=0;
  1880. /* determine requested digits and form */
  1881. if (set.lostDigits)
  1882. this.checkdigits(rhs,set.digits);
  1883. lhs=this; // name for clarity and proxy
  1884. /* Quick exit for add floating 0 */
  1885. // plus() will optimize to return same object if possible
  1886. if (lhs.ind==0)
  1887. if (set.form!=MathContext.prototype.PLAIN)
  1888. return rhs.plus(set);
  1889. if (rhs.ind==0)
  1890. if (set.form!=MathContext.prototype.PLAIN)
  1891. return lhs.plus(set);
  1892. /* Prepare numbers (round, unless unlimited precision) */
  1893. reqdig=set.digits; // local copy (heavily used)
  1894. if (reqdig>0)
  1895. {
  1896. if (lhs.mant.length>reqdig)
  1897. lhs=this.clone(lhs).round(set);
  1898. if (rhs.mant.length>reqdig)
  1899. rhs=this.clone(rhs).round(set);
  1900. // [we could reuse the new LHS for result in this case]
  1901. }
  1902. res=new BigDecimal(); // build result here
  1903. /* Now see how much we have to pad or truncate lhs or rhs in order
  1904. to align the numbers. If one number is much larger than the
  1905. other, then the smaller cannot affect the answer [but we may
  1906. still need to pad with up to DIGITS trailing zeros]. */
  1907. // Note sign may be 0 if digits (reqdig) is 0
  1908. // usel and user will be the byte arrays passed to the adder; we'll
  1909. // use them on all paths except quick exits
  1910. usel=lhs.mant;
  1911. usellen=lhs.mant.length;
  1912. user=rhs.mant;
  1913. userlen=rhs.mant.length;
  1914. {padder:do{/*select*/
  1915. if (lhs.exp==rhs.exp)
  1916. {/* no padding needed */
  1917. // This is the most common, and fastest, path
  1918. res.exp=lhs.exp;
  1919. }
  1920. else if (lhs.exp>rhs.exp)
  1921. { // need to pad lhs and/or truncate rhs
  1922. newlen=(usellen+lhs.exp)-rhs.exp;
  1923. /* If, after pad, lhs would be longer than rhs by digits+1 or
  1924. more (and digits>0) then rhs cannot affect answer, so we only
  1925. need to pad up to a length of DIGITS+1. */
  1926. if (newlen>=((userlen+reqdig)+1))
  1927. if (reqdig>0)
  1928. {
  1929. // LHS is sufficient
  1930. res.mant=usel;
  1931. res.exp=lhs.exp;
  1932. res.ind=lhs.ind;
  1933. if (usellen<reqdig)
  1934. { // need 0 padding
  1935. res.mant=this.extend(lhs.mant,reqdig);
  1936. res.exp=res.exp-((reqdig-usellen));
  1937. }
  1938. return res.finish(set,false);
  1939. }
  1940. // RHS may affect result
  1941. res.exp=rhs.exp; // expected final exponent
  1942. if (newlen>(reqdig+1))
  1943. if (reqdig>0)
  1944. {
  1945. // LHS will be max; RHS truncated
  1946. tlen=(newlen-reqdig)-1; // truncation length
  1947. userlen=userlen-tlen;
  1948. res.exp=res.exp+tlen;
  1949. newlen=reqdig+1;
  1950. }
  1951. if (newlen>usellen)
  1952. usellen=newlen; // need to pad LHS
  1953. }
  1954. else{ // need to pad rhs and/or truncate lhs
  1955. newlen=(userlen+rhs.exp)-lhs.exp;
  1956. if (newlen>=((usellen+reqdig)+1))
  1957. if (reqdig>0)
  1958. {
  1959. // RHS is sufficient
  1960. res.mant=user;
  1961. res.exp=rhs.exp;
  1962. res.ind=rhs.ind;
  1963. if (userlen<reqdig)
  1964. { // need 0 padding
  1965. res.mant=this.extend(rhs.mant,reqdig);
  1966. res.exp=res.exp-((reqdig-userlen));
  1967. }
  1968. return res.finish(set,false);
  1969. }
  1970. // LHS may affect result
  1971. res.exp=lhs.exp; // expected final exponent
  1972. if (newlen>(reqdig+1))
  1973. if (reqdig>0)
  1974. {
  1975. // RHS will be max; LHS truncated
  1976. tlen=(newlen-reqdig)-1; // truncation length
  1977. usellen=usellen-tlen;
  1978. res.exp=res.exp+tlen;
  1979. newlen=reqdig+1;
  1980. }
  1981. if (newlen>userlen)
  1982. userlen=newlen; // need to pad RHS
  1983. }
  1984. }while(false);}/*padder*/
  1985. /* OK, we have aligned mantissas. Now add or subtract. */
  1986. // 1998.06.27 Sign may now be 0 [e.g., 0.000] .. treat as positive
  1987. // 1999.05.27 Allow for 00 on lhs [is not larger than 2 on rhs]
  1988. // 1999.07.10 Allow for 00 on rhs [is not larger than 2 on rhs]
  1989. if (lhs.ind==this.iszero)
  1990. res.ind=this.ispos;
  1991. else
  1992. res.ind=lhs.ind; // likely sign, all paths
  1993. if (((lhs.ind==this.isneg)?1:0)==((rhs.ind==this.isneg)?1:0)) // same sign, 0 non-negative
  1994. mult=1;
  1995. else
  1996. {signdiff:do{ // different signs, so subtraction is needed
  1997. mult=-1; // will cause subtract
  1998. /* Before we can subtract we must determine which is the larger,
  1999. as our add/subtract routine only handles non-negative results
  2000. so we may need to swap the operands. */
  2001. {swaptest:do{/*select*/
  2002. if (rhs.ind==this.iszero)
  2003. {} // original A bigger
  2004. else if ((usellen<userlen)||(lhs.ind==this.iszero))
  2005. { // original B bigger
  2006. t=usel;
  2007. usel=user;
  2008. user=t; // swap
  2009. tlen=usellen;
  2010. usellen=userlen;
  2011. userlen=tlen; // ..
  2012. res.ind=-res.ind; // and set sign
  2013. }
  2014. else if (usellen>userlen)
  2015. {} // original A bigger
  2016. else{
  2017. {/* logical lengths the same */ // need compare
  2018. /* may still need to swap: compare the strings */
  2019. ia=0;
  2020. ib=0;
  2021. ea=usel.length-1;
  2022. eb=user.length-1;
  2023. {compare:for(;;){
  2024. if (ia<=ea)
  2025. ca=usel[ia];
  2026. else
  2027. {
  2028. if (ib>eb)
  2029. {/* identical */
  2030. if (set.form!=MathContext.prototype.PLAIN)
  2031. return this.ZERO;
  2032. // [if PLAIN we must do the subtract, in case of 0.000 results]
  2033. break compare;
  2034. }
  2035. ca=0;
  2036. }
  2037. if (ib<=eb)
  2038. cb=user[ib];
  2039. else
  2040. cb=0;
  2041. if (ca!=cb)
  2042. {
  2043. if (ca<cb)
  2044. {/* swap needed */
  2045. t=usel;
  2046. usel=user;
  2047. user=t; // swap
  2048. tlen=usellen;
  2049. usellen=userlen;
  2050. userlen=tlen; // ..
  2051. res.ind=-res.ind;
  2052. }
  2053. break compare;
  2054. }
  2055. /* mantissas the same, so far */
  2056. ia++;
  2057. ib++;
  2058. }
  2059. }/*compare*/
  2060. } // lengths the same
  2061. }
  2062. }while(false);}/*swaptest*/
  2063. }while(false);}/*signdiff*/
  2064. /* here, A is > B if subtracting */
  2065. // add [A+B*1] or subtract [A+(B*-1)]
  2066. res.mant=this.byteaddsub(usel,usellen,user,userlen,mult,false);
  2067. // [reuse possible only after chop; accounting makes not worthwhile]
  2068. // Finish() rounds before stripping leading 0's, then sets form, etc.
  2069. return res.finish(set,false);
  2070. }
  2071. /**
  2072. * Compares this <code>BigDecimal</code> to another, using unlimited
  2073. * precision.
  2074. * <p>
  2075. * The same as {@link #compareTo(BigDecimal, MathContext)},
  2076. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2077. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2078. *
  2079. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2080. * the comparison.
  2081. * @return An <code>int</code> whose value is -1, 0, or 1 as
  2082. * <code>this</code> is numerically less than, equal to,
  2083. * or greater than <code>rhs</code>.
  2084. * @see #compareTo(Object)
  2085. * @stable ICU 2.0
  2086. */
  2087. //--public int compareTo(com.ibm.icu.math.BigDecimal rhs){
  2088. //-- return this.compareTo(rhs,plainMC);
  2089. //-- }
  2090. /**
  2091. * Compares this <code>BigDecimal</code> to another.
  2092. * <p>
  2093. * Implements numeric comparison,
  2094. * (as defined in the decimal documentation, see {@link BigDecimal
  2095. * class header}),
  2096. * and returns a result of type <code>int</code>.
  2097. * <p>
  2098. * The result will be:
  2099. * <table cellpadding=2><tr>
  2100. * <td align=right><b>-1</b></td>
  2101. * <td>if the current object is less than the first parameter</td>
  2102. * </tr><tr>
  2103. * <td align=right><b>0</b></td>
  2104. * <td>if the current object is equal to the first parameter</td>
  2105. * </tr><tr>
  2106. * <td align=right><b>1</b></td>
  2107. * <td>if the current object is greater than the first parameter.</td>
  2108. * </tr></table>
  2109. * <p>
  2110. * A {@link #compareTo(Object)} method is also provided.
  2111. *
  2112. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2113. * the comparison.
  2114. * @param set The <code>MathContext</code> arithmetic settings.
  2115. * @return An <code>int</code> whose value is -1, 0, or 1 as
  2116. * <code>this</code> is numerically less than, equal to,
  2117. * or greater than <code>rhs</code>.
  2118. * @see #compareTo(Object)
  2119. * @stable ICU 2.0
  2120. */
  2121. //public int compareTo(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2122. function compareTo() {
  2123. var set;
  2124. if (compareTo.arguments.length == 2)
  2125. {
  2126. set = compareTo.arguments[1];
  2127. }
  2128. else if (compareTo.arguments.length == 1)
  2129. {
  2130. set = this.plainMC;
  2131. }
  2132. else
  2133. {
  2134. throw "compareTo(): " + compareTo.arguments.length + " arguments given; expected 1 or 2";
  2135. }
  2136. var rhs = compareTo.arguments[0];
  2137. //--int thislength=0;
  2138. var thislength=0;
  2139. //--int i=0;
  2140. var i=0;
  2141. //--com.ibm.icu.math.BigDecimal newrhs;
  2142. var newrhs;
  2143. // rhs=null will raise NullPointerException, as per Comparable interface
  2144. if (set.lostDigits)
  2145. this.checkdigits(rhs,set.digits);
  2146. // [add will recheck in slowpath cases .. but would report -rhs]
  2147. if ((this.ind==rhs.ind)&&(this.exp==rhs.exp))
  2148. {
  2149. /* sign & exponent the same [very common] */
  2150. thislength=this.mant.length;
  2151. if (thislength<rhs.mant.length)
  2152. return -this.ind;
  2153. if (thislength>rhs.mant.length)
  2154. return this.ind;
  2155. /* lengths are the same; we can do a straight mantissa compare
  2156. unless maybe rounding [rounding is very unusual] */
  2157. if ((thislength<=set.digits)||(set.digits==0))
  2158. {
  2159. {var $6=thislength;i=0;i:for(;$6>0;$6--,i++){
  2160. if (this.mant[i]<rhs.mant[i])
  2161. return -this.ind;
  2162. if (this.mant[i]>rhs.mant[i])
  2163. return this.ind;
  2164. }
  2165. }/*i*/
  2166. return 0; // identical
  2167. }
  2168. /* drop through for full comparison */
  2169. }
  2170. else
  2171. {
  2172. /* More fastpaths possible */
  2173. if (this.ind<rhs.ind)
  2174. return -1;
  2175. if (this.ind>rhs.ind)
  2176. return 1;
  2177. }
  2178. /* carry out a subtract to make the comparison */
  2179. newrhs=this.clone(rhs); // safe copy
  2180. newrhs.ind=-newrhs.ind; // prepare to subtract
  2181. return this.add(newrhs,set).ind; // add, and return sign of result
  2182. }
  2183. /**
  2184. * Returns a plain <code>BigDecimal</code> whose value is
  2185. * <code>this/rhs</code>, using fixed point arithmetic.
  2186. * <p>
  2187. * The same as {@link #divide(BigDecimal, int)},
  2188. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2189. * and the rounding mode is {@link MathContext#ROUND_HALF_UP}.
  2190. *
  2191. * The length of the decimal part (the scale) of the result will be
  2192. * the same as the scale of the current object, if the latter were
  2193. * formatted without exponential notation.
  2194. *
  2195. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2196. * the division.
  2197. * @return A plain <code>BigDecimal</code> whose value is
  2198. * <code>this/rhs</code>, using fixed point arithmetic.
  2199. * @throws ArithmeticException if <code>rhs</code> is zero.
  2200. * @stable ICU 2.0
  2201. */
  2202. //--public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs){
  2203. //-- return this.dodivide('D',rhs,plainMC,-1);
  2204. //-- }
  2205. /**
  2206. * Returns a plain <code>BigDecimal</code> whose value is
  2207. * <code>this/rhs</code>, using fixed point arithmetic and a
  2208. * rounding mode.
  2209. * <p>
  2210. * The same as {@link #divide(BigDecimal, int, int)},
  2211. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2212. * and the second parameter is <code>this.scale()</code>, and
  2213. * the third is <code>round</code>.
  2214. * <p>
  2215. * The length of the decimal part (the scale) of the result will
  2216. * therefore be the same as the scale of the current object, if the
  2217. * latter were formatted without exponential notation.
  2218. * <p>
  2219. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2220. * the division.
  2221. * @param round The <code>int</code> rounding mode to be used for
  2222. * the division (see the {@link MathContext} class).
  2223. * @return A plain <code>BigDecimal</code> whose value is
  2224. * <code>this/rhs</code>, using fixed point arithmetic
  2225. * and the specified rounding mode.
  2226. * @throws IllegalArgumentException if <code>round</code> is not a
  2227. * valid rounding mode.
  2228. * @throws ArithmeticException if <code>rhs</code> is zero.
  2229. * @throws ArithmeticException if <code>round</code> is {@link
  2230. * MathContext#ROUND_UNNECESSARY} and
  2231. * <code>this.scale()</code> is insufficient to
  2232. * represent the result exactly.
  2233. * @stable ICU 2.0
  2234. */
  2235. //--public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs,int round){
  2236. //-- com.ibm.icu.math.MathContext set;
  2237. //-- set=new com.ibm.icu.math.MathContext(0,com.ibm.icu.math.MathContext.PLAIN,false,round); // [checks round, too]
  2238. //-- return this.dodivide('D',rhs,set,-1); // take scale from LHS
  2239. //-- }
  2240. /**
  2241. * Returns a plain <code>BigDecimal</code> whose value is
  2242. * <code>this/rhs</code>, using fixed point arithmetic and a
  2243. * given scale and rounding mode.
  2244. * <p>
  2245. * The same as {@link #divide(BigDecimal, MathContext)},
  2246. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2247. * <code>new MathContext(0, MathContext.PLAIN, false, round)</code>,
  2248. * except that the length of the decimal part (the scale) to be used
  2249. * for the result is explicit rather than being taken from
  2250. * <code>this</code>.
  2251. * <p>
  2252. * The length of the decimal part (the scale) of the result will be
  2253. * the same as the scale of the current object, if the latter were
  2254. * formatted without exponential notation.
  2255. * <p>
  2256. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2257. * the division.
  2258. * @param scale The <code>int</code> scale to be used for the result.
  2259. * @param round The <code>int</code> rounding mode to be used for
  2260. * the division (see the {@link MathContext} class).
  2261. * @return A plain <code>BigDecimal</code> whose value is
  2262. * <code>this/rhs</code>, using fixed point arithmetic
  2263. * and the specified rounding mode.
  2264. * @throws IllegalArgumentException if <code>round</code> is not a
  2265. * valid rounding mode.
  2266. * @throws ArithmeticException if <code>rhs</code> is zero.
  2267. * @throws ArithmeticException if <code>scale</code> is negative.
  2268. * @throws ArithmeticException if <code>round</code> is {@link
  2269. * MathContext#ROUND_UNNECESSARY} and <code>scale</code>
  2270. * is insufficient to represent the result exactly.
  2271. * @stable ICU 2.0
  2272. */
  2273. //--public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs,int scale,int round){
  2274. //-- com.ibm.icu.math.MathContext set;
  2275. //-- if (scale<0)
  2276. //-- throw new java.lang.ArithmeticException("Negative scale:"+" "+scale);
  2277. //-- set=new com.ibm.icu.math.MathContext(0,com.ibm.icu.math.MathContext.PLAIN,false,round); // [checks round]
  2278. //-- return this.dodivide('D',rhs,set,scale);
  2279. //-- }
  2280. /**
  2281. * Returns a <code>BigDecimal</code> whose value is <code>this/rhs</code>.
  2282. * <p>
  2283. * Implements the division (<b><code>/</code></b>) operator
  2284. * (as defined in the decimal documentation, see {@link BigDecimal
  2285. * class header}),
  2286. * and returns the result as a <code>BigDecimal</code> object.
  2287. *
  2288. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2289. * the division.
  2290. * @param set The <code>MathContext</code> arithmetic settings.
  2291. * @return A <code>BigDecimal</code> whose value is
  2292. * <code>this/rhs</code>.
  2293. * @throws ArithmeticException if <code>rhs</code> is zero.
  2294. * @stable ICU 2.0
  2295. */
  2296. //--public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2297. function divide() {
  2298. var set;
  2299. var scale = -1;
  2300. if (divide.arguments.length == 2)
  2301. {
  2302. if (typeof divide.arguments[1] == 'number')
  2303. {
  2304. set=new MathContext(0,MathContext.prototype.PLAIN,false,divide.arguments[1]); // [checks round, too]
  2305. }
  2306. else
  2307. {
  2308. set = divide.arguments[1];
  2309. }
  2310. }
  2311. else if (divide.arguments.length == 3)
  2312. {
  2313. scale = divide.arguments[1];
  2314. if (scale<0)
  2315. throw "divide(): Negative scale: "+scale;
  2316. set=new MathContext(0,MathContext.prototype.PLAIN,false,divide.arguments[2]); // [checks round]
  2317. }
  2318. else if (divide.arguments.length == 1)
  2319. {
  2320. set = this.plainMC;
  2321. }
  2322. else
  2323. {
  2324. throw "divide(): " + divide.arguments.length + " arguments given; expected between 1 and 3";
  2325. }
  2326. var rhs = divide.arguments[0];
  2327. return this.dodivide('D',rhs,set,scale);
  2328. }
  2329. /**
  2330. * Returns a plain <code>BigDecimal</code> whose value is the integer
  2331. * part of <code>this/rhs</code>.
  2332. * <p>
  2333. * The same as {@link #divideInteger(BigDecimal, MathContext)},
  2334. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2335. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2336. *
  2337. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2338. * the integer division.
  2339. * @return A <code>BigDecimal</code> whose value is the integer
  2340. * part of <code>this/rhs</code>.
  2341. * @throws ArithmeticException if <code>rhs</code> is zero.
  2342. * @stable ICU 2.0
  2343. */
  2344. //--public com.ibm.icu.math.BigDecimal divideInteger(com.ibm.icu.math.BigDecimal rhs){
  2345. //-- // scale 0 to drop .000 when plain
  2346. //-- return this.dodivide('I',rhs,plainMC,0);
  2347. //-- }
  2348. /**
  2349. * Returns a <code>BigDecimal</code> whose value is the integer
  2350. * part of <code>this/rhs</code>.
  2351. * <p>
  2352. * Implements the integer division operator
  2353. * (as defined in the decimal documentation, see {@link BigDecimal
  2354. * class header}),
  2355. * and returns the result as a <code>BigDecimal</code> object.
  2356. *
  2357. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2358. * the integer division.
  2359. * @param set The <code>MathContext</code> arithmetic settings.
  2360. * @return A <code>BigDecimal</code> whose value is the integer
  2361. * part of <code>this/rhs</code>.
  2362. * @throws ArithmeticException if <code>rhs</code> is zero.
  2363. * @throws ArithmeticException if the result will not fit in the
  2364. * number of digits specified for the context.
  2365. * @stable ICU 2.0
  2366. */
  2367. //--public com.ibm.icu.math.BigDecimal divideInteger(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2368. function divideInteger() {
  2369. var set;
  2370. if (divideInteger.arguments.length == 2)
  2371. {
  2372. set = divideInteger.arguments[1];
  2373. }
  2374. else if (divideInteger.arguments.length == 1)
  2375. {
  2376. set = this.plainMC;
  2377. }
  2378. else
  2379. {
  2380. throw "divideInteger(): " + divideInteger.arguments.length + " arguments given; expected 1 or 2";
  2381. }
  2382. var rhs = divideInteger.arguments[0];
  2383. // scale 0 to drop .000 when plain
  2384. return this.dodivide('I',rhs,set,0);
  2385. }
  2386. /**
  2387. * Returns a plain <code>BigDecimal</code> whose value is
  2388. * the maximum of <code>this</code> and <code>rhs</code>.
  2389. * <p>
  2390. * The same as {@link #max(BigDecimal, MathContext)},
  2391. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2392. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2393. *
  2394. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2395. * the comparison.
  2396. * @return A <code>BigDecimal</code> whose value is
  2397. * the maximum of <code>this</code> and <code>rhs</code>.
  2398. * @stable ICU 2.0
  2399. */
  2400. //--public com.ibm.icu.math.BigDecimal max(com.ibm.icu.math.BigDecimal rhs){
  2401. //-- return this.max(rhs,plainMC);
  2402. //-- }
  2403. /**
  2404. * Returns a <code>BigDecimal</code> whose value is
  2405. * the maximum of <code>this</code> and <code>rhs</code>.
  2406. * <p>
  2407. * Returns the larger of the current object and the first parameter.
  2408. * <p>
  2409. * If calling the {@link #compareTo(BigDecimal, MathContext)} method
  2410. * with the same parameters would return <code>1</code> or
  2411. * <code>0</code>, then the result of calling the
  2412. * {@link #plus(MathContext)} method on the current object (using the
  2413. * same <code>MathContext</code> parameter) is returned.
  2414. * Otherwise, the result of calling the {@link #plus(MathContext)}
  2415. * method on the first parameter object (using the same
  2416. * <code>MathContext</code> parameter) is returned.
  2417. *
  2418. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2419. * the comparison.
  2420. * @param set The <code>MathContext</code> arithmetic settings.
  2421. * @return A <code>BigDecimal</code> whose value is
  2422. * the maximum of <code>this</code> and <code>rhs</code>.
  2423. * @stable ICU 2.0
  2424. */
  2425. //--public com.ibm.icu.math.BigDecimal max(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2426. function max() {
  2427. var set;
  2428. if (max.arguments.length == 2)
  2429. {
  2430. set = max.arguments[1];
  2431. }
  2432. else if (max.arguments.length == 1)
  2433. {
  2434. set = this.plainMC;
  2435. }
  2436. else
  2437. {
  2438. throw "max(): " + max.arguments.length + " arguments given; expected 1 or 2";
  2439. }
  2440. var rhs = max.arguments[0];
  2441. if ((this.compareTo(rhs,set))>=0)
  2442. return this.plus(set);
  2443. else
  2444. return rhs.plus(set);
  2445. }
  2446. /**
  2447. * Returns a plain <code>BigDecimal</code> whose value is
  2448. * the minimum of <code>this</code> and <code>rhs</code>.
  2449. * <p>
  2450. * The same as {@link #min(BigDecimal, MathContext)},
  2451. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2452. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2453. *
  2454. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2455. * the comparison.
  2456. * @return A <code>BigDecimal</code> whose value is
  2457. * the minimum of <code>this</code> and <code>rhs</code>.
  2458. * @stable ICU 2.0
  2459. */
  2460. //--public com.ibm.icu.math.BigDecimal min(com.ibm.icu.math.BigDecimal rhs){
  2461. //-- return this.min(rhs,plainMC);
  2462. //-- }
  2463. /**
  2464. * Returns a <code>BigDecimal</code> whose value is
  2465. * the minimum of <code>this</code> and <code>rhs</code>.
  2466. * <p>
  2467. * Returns the smaller of the current object and the first parameter.
  2468. * <p>
  2469. * If calling the {@link #compareTo(BigDecimal, MathContext)} method
  2470. * with the same parameters would return <code>-1</code> or
  2471. * <code>0</code>, then the result of calling the
  2472. * {@link #plus(MathContext)} method on the current object (using the
  2473. * same <code>MathContext</code> parameter) is returned.
  2474. * Otherwise, the result of calling the {@link #plus(MathContext)}
  2475. * method on the first parameter object (using the same
  2476. * <code>MathContext</code> parameter) is returned.
  2477. *
  2478. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2479. * the comparison.
  2480. * @param set The <code>MathContext</code> arithmetic settings.
  2481. * @return A <code>BigDecimal</code> whose value is
  2482. * the minimum of <code>this</code> and <code>rhs</code>.
  2483. * @stable ICU 2.0
  2484. */
  2485. //--public com.ibm.icu.math.BigDecimal min(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2486. function min() {
  2487. var set;
  2488. if (min.arguments.length == 2)
  2489. {
  2490. set = min.arguments[1];
  2491. }
  2492. else if (min.arguments.length == 1)
  2493. {
  2494. set = this.plainMC;
  2495. }
  2496. else
  2497. {
  2498. throw "min(): " + min.arguments.length + " arguments given; expected 1 or 2";
  2499. }
  2500. var rhs = min.arguments[0];
  2501. if ((this.compareTo(rhs,set))<=0)
  2502. return this.plus(set);
  2503. else
  2504. return rhs.plus(set);
  2505. }
  2506. /**
  2507. * Returns a plain <code>BigDecimal</code> whose value is
  2508. * <code>this*rhs</code>, using fixed point arithmetic.
  2509. * <p>
  2510. * The same as {@link #add(BigDecimal, MathContext)},
  2511. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2512. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2513. * <p>
  2514. * The length of the decimal part (the scale) of the result will be
  2515. * the sum of the scales of the operands, if they were formatted
  2516. * without exponential notation.
  2517. *
  2518. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2519. * the multiplication.
  2520. * @return A <code>BigDecimal</code> whose value is
  2521. * <code>this*rhs</code>, using fixed point arithmetic.
  2522. * @stable ICU 2.0
  2523. */
  2524. //--public com.ibm.icu.math.BigDecimal multiply(com.ibm.icu.math.BigDecimal rhs){
  2525. //-- return this.multiply(rhs,plainMC);
  2526. //-- }
  2527. /**
  2528. * Returns a <code>BigDecimal</code> whose value is <code>this*rhs</code>.
  2529. * <p>
  2530. * Implements the multiplication (<b><code>*</code></b>) operator
  2531. * (as defined in the decimal documentation, see {@link BigDecimal
  2532. * class header}),
  2533. * and returns the result as a <code>BigDecimal</code> object.
  2534. *
  2535. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2536. * the multiplication.
  2537. * @param set The <code>MathContext</code> arithmetic settings.
  2538. * @return A <code>BigDecimal</code> whose value is
  2539. * <code>this*rhs</code>.
  2540. * @stable ICU 2.0
  2541. */
  2542. //--public com.ibm.icu.math.BigDecimal multiply(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2543. function multiply() {
  2544. var set;
  2545. if (multiply.arguments.length == 2)
  2546. {
  2547. set = multiply.arguments[1];
  2548. }
  2549. else if (multiply.arguments.length == 1)
  2550. {
  2551. set = this.plainMC;
  2552. }
  2553. else
  2554. {
  2555. throw "multiply(): " + multiply.arguments.length + " arguments given; expected 1 or 2";
  2556. }
  2557. var rhs = multiply.arguments[0];
  2558. //--com.ibm.icu.math.BigDecimal lhs;
  2559. var lhs;
  2560. //--int padding;
  2561. var padding;
  2562. //--int reqdig;
  2563. var reqdig;
  2564. //--byte multer[]=null;
  2565. var multer=null;
  2566. //--byte multand[]=null;
  2567. var multand=null;
  2568. //--int multandlen;
  2569. var multandlen;
  2570. //--int acclen=0;
  2571. var acclen=0;
  2572. //--com.ibm.icu.math.BigDecimal res;
  2573. var res;
  2574. //--byte acc[];
  2575. var acc;
  2576. //--int n=0;
  2577. var n=0;
  2578. //--byte mult=0;
  2579. var mult=0;
  2580. if (set.lostDigits)
  2581. this.checkdigits(rhs,set.digits);
  2582. lhs=this; // name for clarity and proxy
  2583. /* Prepare numbers (truncate, unless unlimited precision) */
  2584. padding=0; // trailing 0's to add
  2585. reqdig=set.digits; // local copy
  2586. if (reqdig>0)
  2587. {
  2588. if (lhs.mant.length>reqdig)
  2589. lhs=this.clone(lhs).round(set);
  2590. if (rhs.mant.length>reqdig)
  2591. rhs=this.clone(rhs).round(set);
  2592. // [we could reuse the new LHS for result in this case]
  2593. }
  2594. else
  2595. {/* unlimited */
  2596. // fixed point arithmetic will want every trailing 0; we add these
  2597. // after the calculation rather than before, for speed.
  2598. if (lhs.exp>0)
  2599. padding=padding+lhs.exp;
  2600. if (rhs.exp>0)
  2601. padding=padding+rhs.exp;
  2602. }
  2603. // For best speed, as in DMSRCN, we use the shorter number as the
  2604. // multiplier and the longer as the multiplicand.
  2605. // 1999.12.22: We used to special case when the result would fit in
  2606. // a long, but with Java 1.3 this gave no advantage.
  2607. if (lhs.mant.length<rhs.mant.length)
  2608. {
  2609. multer=lhs.mant;
  2610. multand=rhs.mant;
  2611. }
  2612. else
  2613. {
  2614. multer=rhs.mant;
  2615. multand=lhs.mant;
  2616. }
  2617. /* Calculate how long result byte array will be */
  2618. multandlen=(multer.length+multand.length)-1; // effective length
  2619. // optimize for 75% of the cases where a carry is expected...
  2620. if ((multer[0]*multand[0])>9)
  2621. acclen=multandlen+1;
  2622. else
  2623. acclen=multandlen;
  2624. /* Now the main long multiplication loop */
  2625. res=new BigDecimal(); // where we'll build result
  2626. acc=this.createArrayWithZeros(acclen); // accumulator, all zeros
  2627. // 1998.07.01: calculate from left to right so that accumulator goes
  2628. // to likely final length on first addition; this avoids a one-digit
  2629. // extension (and object allocation) each time around the loop.
  2630. // Initial number therefore has virtual zeros added to right.
  2631. {var $7=multer.length;n=0;n:for(;$7>0;$7--,n++){
  2632. mult=multer[n];
  2633. if (mult!=0)
  2634. { // [optimization]
  2635. // accumulate [accumulator is reusable array]
  2636. acc=this.byteaddsub(acc,acc.length,multand,multandlen,mult,true);
  2637. }
  2638. // divide multiplicand by 10 for next digit to right
  2639. multandlen--; // 'virtual length'
  2640. }
  2641. }/*n*/
  2642. res.ind=lhs.ind*rhs.ind; // final sign
  2643. res.exp=(lhs.exp+rhs.exp)-padding; // final exponent
  2644. // [overflow is checked by finish]
  2645. /* add trailing zeros to the result, if necessary */
  2646. if (padding==0)
  2647. res.mant=acc;
  2648. else
  2649. res.mant=this.extend(acc,acc.length+padding); // add trailing 0s
  2650. return res.finish(set,false);
  2651. }
  2652. /**
  2653. * Returns a plain <code>BigDecimal</code> whose value is
  2654. * <code>-this</code>.
  2655. * <p>
  2656. * The same as {@link #negate(MathContext)}, where the context is
  2657. * <code>new MathContext(0, MathContext.PLAIN)</code>.
  2658. * <p>
  2659. * The length of the decimal part (the scale) of the result will be
  2660. * be <code>this.scale()</code>
  2661. *
  2662. *
  2663. * @return A <code>BigDecimal</code> whose value is
  2664. * <code>-this</code>.
  2665. * @stable ICU 2.0
  2666. */
  2667. //--public com.ibm.icu.math.BigDecimal negate(){
  2668. //-- return this.negate(plainMC);
  2669. //-- }
  2670. /**
  2671. * Returns a <code>BigDecimal</code> whose value is <code>-this</code>.
  2672. * <p>
  2673. * Implements the negation (Prefix <b><code>-</code></b>) operator
  2674. * (as defined in the decimal documentation, see {@link BigDecimal
  2675. * class header}),
  2676. * and returns the result as a <code>BigDecimal</code> object.
  2677. *
  2678. * @param set The <code>MathContext</code> arithmetic settings.
  2679. * @return A <code>BigDecimal</code> whose value is
  2680. * <code>-this</code>.
  2681. * @stable ICU 2.0
  2682. */
  2683. //public com.ibm.icu.math.BigDecimal negate(com.ibm.icu.math.MathContext set){
  2684. function negate() {
  2685. var set;
  2686. if (negate.arguments.length == 1)
  2687. {
  2688. set = negate.arguments[0];
  2689. }
  2690. else if (negate.arguments.length == 0)
  2691. {
  2692. set = this.plainMC;
  2693. }
  2694. else
  2695. {
  2696. throw "negate(): " + negate.arguments.length + " arguments given; expected 0 or 1";
  2697. }
  2698. //--com.ibm.icu.math.BigDecimal res;
  2699. var res;
  2700. // Originally called minus(), changed to matched Java precedents
  2701. // This simply clones, flips the sign, and possibly rounds
  2702. if (set.lostDigits)
  2703. this.checkdigits(null,set.digits);
  2704. res=this.clone(this); // safe copy
  2705. res.ind=-res.ind;
  2706. return res.finish(set,false);
  2707. }
  2708. /**
  2709. * Returns a plain <code>BigDecimal</code> whose value is
  2710. * <code>+this</code>.
  2711. * Note that <code>this</code> is not necessarily a
  2712. * plain <code>BigDecimal</code>, but the result will always be.
  2713. * <p>
  2714. * The same as {@link #plus(MathContext)}, where the context is
  2715. * <code>new MathContext(0, MathContext.PLAIN)</code>.
  2716. * <p>
  2717. * The length of the decimal part (the scale) of the result will be
  2718. * be <code>this.scale()</code>
  2719. *
  2720. * @return A <code>BigDecimal</code> whose value is
  2721. * <code>+this</code>.
  2722. * @stable ICU 2.0
  2723. */
  2724. //--public com.ibm.icu.math.BigDecimal plus(){
  2725. //-- return this.plus(plainMC);
  2726. //-- }
  2727. /**
  2728. * Returns a <code>BigDecimal</code> whose value is
  2729. * <code>+this</code>.
  2730. * <p>
  2731. * Implements the plus (Prefix <b><code>+</code></b>) operator
  2732. * (as defined in the decimal documentation, see {@link BigDecimal
  2733. * class header}),
  2734. * and returns the result as a <code>BigDecimal</code> object.
  2735. * <p>
  2736. * This method is useful for rounding or otherwise applying a context
  2737. * to a decimal value.
  2738. *
  2739. * @param set The <code>MathContext</code> arithmetic settings.
  2740. * @return A <code>BigDecimal</code> whose value is
  2741. * <code>+this</code>.
  2742. * @stable ICU 2.0
  2743. */
  2744. //public com.ibm.icu.math.BigDecimal plus(com.ibm.icu.math.MathContext set){
  2745. function plus() {
  2746. var set;
  2747. if (plus.arguments.length == 1)
  2748. {
  2749. set = plus.arguments[0];
  2750. }
  2751. else if (plus.arguments.length == 0)
  2752. {
  2753. set = this.plainMC;
  2754. }
  2755. else
  2756. {
  2757. throw "plus(): " + plus.arguments.length + " arguments given; expected 0 or 1";
  2758. }
  2759. // This clones and forces the result to the new settings
  2760. // May return same object
  2761. if (set.lostDigits)
  2762. this.checkdigits(null,set.digits);
  2763. // Optimization: returns same object for some common cases
  2764. if (set.form==MathContext.prototype.PLAIN)
  2765. if (this.form==MathContext.prototype.PLAIN)
  2766. {
  2767. if (this.mant.length<=set.digits)
  2768. return this;
  2769. if (set.digits==0)
  2770. return this;
  2771. }
  2772. return this.clone(this).finish(set,false);
  2773. }
  2774. /**
  2775. * Returns a plain <code>BigDecimal</code> whose value is
  2776. * <code>this**rhs</code>, using fixed point arithmetic.
  2777. * <p>
  2778. * The same as {@link #pow(BigDecimal, MathContext)},
  2779. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2780. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2781. * <p>
  2782. * The parameter is the power to which the <code>this</code> will be
  2783. * raised; it must be in the range 0 through 999999999, and must
  2784. * have a decimal part of zero. Note that these restrictions may be
  2785. * removed in the future, so they should not be used as a test for a
  2786. * whole number.
  2787. * <p>
  2788. * In addition, the power must not be negative, as no
  2789. * <code>MathContext</code> is used and so the result would then
  2790. * always be 0.
  2791. *
  2792. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2793. * the operation (the power).
  2794. * @return A <code>BigDecimal</code> whose value is
  2795. * <code>this**rhs</code>, using fixed point arithmetic.
  2796. * @throws ArithmeticException if <code>rhs</code> is out of range or
  2797. * is not a whole number.
  2798. * @stable ICU 2.0
  2799. */
  2800. //--public com.ibm.icu.math.BigDecimal pow(com.ibm.icu.math.BigDecimal rhs){
  2801. //-- return this.pow(rhs,plainMC);
  2802. //-- }
  2803. // The name for this method is inherited from the precedent set by the
  2804. // BigInteger and Math classes.
  2805. /**
  2806. * Returns a <code>BigDecimal</code> whose value is <code>this**rhs</code>.
  2807. * <p>
  2808. * Implements the power (<b><code>**</code></b>) operator
  2809. * (as defined in the decimal documentation, see {@link BigDecimal
  2810. * class header}),
  2811. * and returns the result as a <code>BigDecimal</code> object.
  2812. * <p>
  2813. * The first parameter is the power to which the <code>this</code>
  2814. * will be raised; it must be in the range -999999999 through
  2815. * 999999999, and must have a decimal part of zero. Note that these
  2816. * restrictions may be removed in the future, so they should not be
  2817. * used as a test for a whole number.
  2818. * <p>
  2819. * If the <code>digits</code> setting of the <code>MathContext</code>
  2820. * parameter is 0, the power must be zero or positive.
  2821. *
  2822. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2823. * the operation (the power).
  2824. * @param set The <code>MathContext</code> arithmetic settings.
  2825. * @return A <code>BigDecimal</code> whose value is
  2826. * <code>this**rhs</code>.
  2827. * @throws ArithmeticException if <code>rhs</code> is out of range or
  2828. * is not a whole number.
  2829. * @stable ICU 2.0
  2830. */
  2831. //--public com.ibm.icu.math.BigDecimal pow(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2832. function pow() {
  2833. var set;
  2834. if (pow.arguments.length == 2)
  2835. {
  2836. set = pow.arguments[1];
  2837. }
  2838. else if (pow.arguments.length == 1)
  2839. {
  2840. set = this.plainMC;
  2841. }
  2842. else
  2843. {
  2844. throw "pow(): " + pow.arguments.length + " arguments given; expected 1 or 2";
  2845. }
  2846. var rhs = pow.arguments[0];
  2847. //--int n;
  2848. var n;
  2849. //--com.ibm.icu.math.BigDecimal lhs;
  2850. var lhs;
  2851. //--int reqdig;
  2852. var reqdig;
  2853. //-- int workdigits=0;
  2854. var workdigits=0;
  2855. //--int L=0;
  2856. var L=0;
  2857. //--com.ibm.icu.math.MathContext workset;
  2858. var workset;
  2859. //--com.ibm.icu.math.BigDecimal res;
  2860. var res;
  2861. //--boolean seenbit;
  2862. var seenbit;
  2863. //--int i=0;
  2864. var i=0;
  2865. if (set.lostDigits)
  2866. this.checkdigits(rhs,set.digits);
  2867. n=rhs.intcheck(this.MinArg,this.MaxArg); // check RHS by the rules
  2868. lhs=this; // clarified name
  2869. reqdig=set.digits; // local copy (heavily used)
  2870. if (reqdig==0)
  2871. {
  2872. if (rhs.ind==this.isneg)
  2873. //--throw new java.lang.ArithmeticException("Negative power:"+" "+rhs.toString());
  2874. throw "pow(): Negative power: " + rhs.toString();
  2875. workdigits=0;
  2876. }
  2877. else
  2878. {/* non-0 digits */
  2879. if ((rhs.mant.length+rhs.exp)>reqdig)
  2880. //--throw new java.lang.ArithmeticException("Too many digits:"+" "+rhs.toString());
  2881. throw "pow(): Too many digits: " + rhs.toString();
  2882. /* Round the lhs to DIGITS if need be */
  2883. if (lhs.mant.length>reqdig)
  2884. lhs=this.clone(lhs).round(set);
  2885. /* L for precision calculation [see ANSI X3.274-1996] */
  2886. L=rhs.mant.length+rhs.exp; // length without decimal zeros/exp
  2887. workdigits=(reqdig+L)+1; // calculate the working DIGITS
  2888. }
  2889. /* Create a copy of set for working settings */
  2890. // Note: no need to check for lostDigits again.
  2891. // 1999.07.17 Note: this construction must follow RHS check
  2892. workset=new MathContext(workdigits,set.form,false,set.roundingMode);
  2893. res=this.ONE; // accumulator
  2894. if (n==0)
  2895. return res; // x**0 == 1
  2896. if (n<0)
  2897. n=-n; // [rhs.ind records the sign]
  2898. seenbit=false; // set once we've seen a 1-bit
  2899. {i=1;i:for(;;i++){ // for each bit [top bit ignored]
  2900. //n=n+n; // shift left 1 bit
  2901. n<<=1;
  2902. if (n<0)
  2903. { // top bit is set
  2904. seenbit=true; // OK, we're off
  2905. res=res.multiply(lhs,workset); // acc=acc*x
  2906. }
  2907. if (i==31)
  2908. break i; // that was the last bit
  2909. if ((!seenbit))
  2910. continue i; // we don't have to square 1
  2911. res=res.multiply(res,workset); // acc=acc*acc [square]
  2912. }
  2913. }/*i*/ // 32 bits
  2914. if (rhs.ind<0) // was a **-n [hence digits>0]
  2915. res=this.ONE.divide(res,workset); // .. so acc=1/acc
  2916. return res.finish(set,true); // round and strip [original digits]
  2917. }
  2918. /**
  2919. * Returns a plain <code>BigDecimal</code> whose value is
  2920. * the remainder of <code>this/rhs</code>, using fixed point arithmetic.
  2921. * <p>
  2922. * The same as {@link #remainder(BigDecimal, MathContext)},
  2923. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2924. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2925. * <p>
  2926. * This is not the modulo operator -- the result may be negative.
  2927. *
  2928. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2929. * the remainder operation.
  2930. * @return A <code>BigDecimal</code> whose value is the remainder
  2931. * of <code>this/rhs</code>, using fixed point arithmetic.
  2932. * @throws ArithmeticException if <code>rhs</code> is zero.
  2933. * @stable ICU 2.0
  2934. */
  2935. //--public com.ibm.icu.math.BigDecimal remainder(com.ibm.icu.math.BigDecimal rhs){
  2936. //-- return this.dodivide('R',rhs,plainMC,-1);
  2937. //-- }
  2938. /**
  2939. * Returns a <code>BigDecimal</code> whose value is the remainder of
  2940. * <code>this/rhs</code>.
  2941. * <p>
  2942. * Implements the remainder operator
  2943. * (as defined in the decimal documentation, see {@link BigDecimal
  2944. * class header}),
  2945. * and returns the result as a <code>BigDecimal</code> object.
  2946. * <p>
  2947. * This is not the modulo operator -- the result may be negative.
  2948. *
  2949. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2950. * the remainder operation.
  2951. * @param set The <code>MathContext</code> arithmetic settings.
  2952. * @return A <code>BigDecimal</code> whose value is the remainder
  2953. * of <code>this+rhs</code>.
  2954. * @throws ArithmeticException if <code>rhs</code> is zero.
  2955. * @throws ArithmeticException if the integer part of the result will
  2956. * not fit in the number of digits specified for the
  2957. * context.
  2958. * @stable ICU 2.0
  2959. */
  2960. //--public com.ibm.icu.math.BigDecimal remainder(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  2961. function remainder() {
  2962. var set;
  2963. if (remainder.arguments.length == 2)
  2964. {
  2965. set = remainder.arguments[1];
  2966. }
  2967. else if (remainder.arguments.length == 1)
  2968. {
  2969. set = this.plainMC;
  2970. }
  2971. else
  2972. {
  2973. throw "remainder(): " + remainder.arguments.length + " arguments given; expected 1 or 2";
  2974. }
  2975. var rhs = remainder.arguments[0];
  2976. return this.dodivide('R',rhs,set,-1);
  2977. }
  2978. /**
  2979. * Returns a plain <code>BigDecimal</code> whose value is
  2980. * <code>this-rhs</code>, using fixed point arithmetic.
  2981. * <p>
  2982. * The same as {@link #subtract(BigDecimal, MathContext)},
  2983. * where the <code>BigDecimal</code> is <code>rhs</code>,
  2984. * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
  2985. * <p>
  2986. * The length of the decimal part (the scale) of the result will be
  2987. * the maximum of the scales of the two operands.
  2988. *
  2989. * @param rhs The <code>BigDecimal</code> for the right hand side of
  2990. * the subtraction.
  2991. * @return A <code>BigDecimal</code> whose value is
  2992. * <code>this-rhs</code>, using fixed point arithmetic.
  2993. * @stable ICU 2.0
  2994. */
  2995. //--public com.ibm.icu.math.BigDecimal subtract(com.ibm.icu.math.BigDecimal rhs){
  2996. //-- return this.subtract(rhs,plainMC);
  2997. //-- }
  2998. /**
  2999. * Returns a <code>BigDecimal</code> whose value is <code>this-rhs</code>.
  3000. * <p>
  3001. * Implements the subtraction (<b><code>-</code></b>) operator
  3002. * (as defined in the decimal documentation, see {@link BigDecimal
  3003. * class header}),
  3004. * and returns the result as a <code>BigDecimal</code> object.
  3005. *
  3006. * @param rhs The <code>BigDecimal</code> for the right hand side of
  3007. * the subtraction.
  3008. * @param set The <code>MathContext</code> arithmetic settings.
  3009. * @return A <code>BigDecimal</code> whose value is
  3010. * <code>this-rhs</code>.
  3011. * @stable ICU 2.0
  3012. */
  3013. //--public com.ibm.icu.math.BigDecimal subtract(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){
  3014. function subtract() {
  3015. var set;
  3016. if (subtract.arguments.length == 2)
  3017. {
  3018. set = subtract.arguments[1];
  3019. }
  3020. else if (subtract.arguments.length == 1)
  3021. {
  3022. set = this.plainMC;
  3023. }
  3024. else
  3025. {
  3026. throw "subtract(): " + subtract.arguments.length + " arguments given; expected 1 or 2";
  3027. }
  3028. var rhs = subtract.arguments[0];
  3029. //--com.ibm.icu.math.BigDecimal newrhs;
  3030. var newrhs;
  3031. if (set.lostDigits)
  3032. this.checkdigits(rhs,set.digits);
  3033. // [add will recheck .. but would report -rhs]
  3034. /* carry out the subtraction */
  3035. // we could fastpath -0, but it is too rare.
  3036. newrhs=this.clone(rhs); // safe copy
  3037. newrhs.ind=-newrhs.ind; // prepare to subtract
  3038. return this.add(newrhs,set); // arithmetic
  3039. }
  3040. /* ---------------------------------------------------------------- */
  3041. /* Other methods */
  3042. /* ---------------------------------------------------------------- */
  3043. /**
  3044. * Converts this <code>BigDecimal</code> to a <code>byte</code>.
  3045. * If the <code>BigDecimal</code> has a non-zero decimal part or is
  3046. * out of the possible range for a <code>byte</code> (8-bit signed
  3047. * integer) result then an <code>ArithmeticException</code> is thrown.
  3048. *
  3049. * @return A <code>byte</code> equal in value to <code>this</code>.
  3050. * @throws ArithmeticException if <code>this</code> has a non-zero
  3051. * decimal part, or will not fit in a <code>byte</code>.
  3052. * @stable ICU 2.0
  3053. */
  3054. //--public byte byteValueExact(){
  3055. //-- int num;
  3056. //-- num=this.intValueExact(); // will check decimal part too
  3057. //-- if ((num>127)|(num<(-128)))
  3058. //-- throw new java.lang.ArithmeticException("Conversion overflow:"+" "+this.toString());
  3059. //-- return (byte)num;
  3060. //-- }
  3061. /**
  3062. * Compares this <code>BigDecimal</code> with the value of the parameter.
  3063. * <p>
  3064. * If the parameter is <code>null</code>, or is not an instance of the
  3065. * <code>BigDecimal</code> type, an exception is thrown.
  3066. * Otherwise, the parameter is cast to type <code>BigDecimal</code>
  3067. * and the result of the {@link #compareTo(BigDecimal)} method,
  3068. * using the cast parameter, is returned.
  3069. * <p>
  3070. * The {@link #compareTo(BigDecimal, MathContext)} method should be
  3071. * used when a <code>MathContext</code> is needed for the comparison.
  3072. *
  3073. * @param rhs The <code>Object</code> for the right hand side of
  3074. * the comparison.
  3075. * @return An <code>int</code> whose value is -1, 0, or 1 as
  3076. * <code>this</code> is numerically less than, equal to,
  3077. * or greater than <code>rhs</code>.
  3078. * @throws ClassCastException if <code>rhs</code> cannot be cast to
  3079. * a <code>BigDecimal</code> object.
  3080. * @see #compareTo(BigDecimal)
  3081. * @stable ICU 2.0
  3082. */
  3083. //--public int compareTo(java.lang.Object rhsobj){
  3084. //-- // the cast in the next line will raise ClassCastException if necessary
  3085. //-- return compareTo((com.ibm.icu.math.BigDecimal)rhsobj,plainMC);
  3086. //-- }
  3087. /**
  3088. * Converts this <code>BigDecimal</code> to a <code>double</code>.
  3089. * If the <code>BigDecimal</code> is out of the possible range for a
  3090. * <code>double</code> (64-bit signed floating point) result then an
  3091. * <code>ArithmeticException</code> is thrown.
  3092. * <p>
  3093. * The double produced is identical to result of expressing the
  3094. * <code>BigDecimal</code> as a <code>String</code> and then
  3095. * converting it using the <code>Double(String)</code> constructor;
  3096. * this can result in values of <code>Double.NEGATIVE_INFINITY</code>
  3097. * or <code>Double.POSITIVE_INFINITY</code>.
  3098. *
  3099. * @return A <code>double</code> corresponding to <code>this</code>.
  3100. * @stable ICU 2.0
  3101. */
  3102. //--public double doubleValue(){
  3103. //-- // We go via a String [as does BigDecimal in JDK 1.2]
  3104. //-- // Next line could possibly raise NumberFormatException
  3105. //-- return java.lang.Double.valueOf(this.toString()).doubleValue();
  3106. //-- }
  3107. /**
  3108. * Compares this <code>BigDecimal</code> with <code>rhs</code> for
  3109. * equality.
  3110. * <p>
  3111. * If the parameter is <code>null</code>, or is not an instance of the
  3112. * BigDecimal type, or is not exactly equal to the current
  3113. * <code>BigDecimal</code> object, then <i>false</i> is returned.
  3114. * Otherwise, <i>true</i> is returned.
  3115. * <p>
  3116. * "Exactly equal", here, means that the <code>String</code>
  3117. * representations of the <code>BigDecimal</code> numbers are
  3118. * identical (they have the same characters in the same sequence).
  3119. * <p>
  3120. * The {@link #compareTo(BigDecimal, MathContext)} method should be
  3121. * used for more general comparisons.
  3122. * @param rhs The <code>Object</code> for the right hand side of
  3123. * the comparison.
  3124. * @return A <code>boolean</code> whose value <i>true</i> if and
  3125. * only if the operands have identical string representations.
  3126. * @throws ClassCastException if <code>rhs</code> cannot be cast to
  3127. * a <code>BigDecimal</code> object.
  3128. * @stable ICU 2.0
  3129. * @see #compareTo(Object)
  3130. * @see #compareTo(BigDecimal)
  3131. * @see #compareTo(BigDecimal, MathContext)
  3132. */
  3133. //--public boolean equals(java.lang.Object obj){
  3134. function equals(obj) {
  3135. //--com.ibm.icu.math.BigDecimal rhs;
  3136. var rhs;
  3137. //--int i=0;
  3138. var i=0;
  3139. //--char lca[]=null;
  3140. var lca=null;
  3141. //--char rca[]=null;
  3142. var rca=null;
  3143. // We are equal iff toString of both are exactly the same
  3144. if (obj==null)
  3145. return false; // not equal
  3146. if ((!(((obj instanceof BigDecimal)))))
  3147. return false; // not a decimal
  3148. rhs=obj; // cast; we know it will work
  3149. if (this.ind!=rhs.ind)
  3150. return false; // different signs never match
  3151. if (((this.mant.length==rhs.mant.length)&&(this.exp==rhs.exp))&&(this.form==rhs.form))
  3152. { // mantissas say all
  3153. // here with equal-length byte arrays to compare
  3154. {var $8=this.mant.length;i=0;i:for(;$8>0;$8--,i++){
  3155. if (this.mant[i]!=rhs.mant[i])
  3156. return false;
  3157. }
  3158. }/*i*/
  3159. }
  3160. else
  3161. { // need proper layout
  3162. lca=this.layout(); // layout to character array
  3163. rca=rhs.layout();
  3164. if (lca.length!=rca.length)
  3165. return false; // mismatch
  3166. // here with equal-length character arrays to compare
  3167. {var $9=lca.length;i=0;i:for(;$9>0;$9--,i++){
  3168. if (lca[i]!=rca[i])
  3169. return false;
  3170. }
  3171. }/*i*/
  3172. }
  3173. return true; // arrays have identical content
  3174. }
  3175. /**
  3176. * Converts this <code>BigDecimal</code> to a <code>float</code>.
  3177. * If the <code>BigDecimal</code> is out of the possible range for a
  3178. * <code>float</code> (32-bit signed floating point) result then an
  3179. * <code>ArithmeticException</code> is thrown.
  3180. * <p>
  3181. * The float produced is identical to result of expressing the
  3182. * <code>BigDecimal</code> as a <code>String</code> and then
  3183. * converting it using the <code>Float(String)</code> constructor;
  3184. * this can result in values of <code>Float.NEGATIVE_INFINITY</code>
  3185. * or <code>Float.POSITIVE_INFINITY</code>.
  3186. *
  3187. * @return A <code>float</code> corresponding to <code>this</code>.
  3188. * @stable ICU 2.0
  3189. */
  3190. //--public float floatValue(){
  3191. //-- return java.lang.Float.valueOf(this.toString()).floatValue();
  3192. //-- }
  3193. /**
  3194. * Returns the <code>String</code> representation of this
  3195. * <code>BigDecimal</code>, modified by layout parameters.
  3196. * <p>
  3197. * <i>This method is provided as a primitive for use by more
  3198. * sophisticated classes, such as <code>DecimalFormat</code>, that
  3199. * can apply locale-sensitive editing of the result. The level of
  3200. * formatting that it provides is a necessary part of the BigDecimal
  3201. * class as it is sensitive to and must follow the calculation and
  3202. * rounding rules for BigDecimal arithmetic.
  3203. * However, if the function is provided elsewhere, it may be removed
  3204. * from this class. </i>
  3205. * <p>
  3206. * The parameters, for both forms of the <code>format</code> method
  3207. * are all of type <code>int</code>.
  3208. * A value of -1 for any parameter indicates that the default action
  3209. * or value for that parameter should be used.
  3210. * <p>
  3211. * The parameters, <code>before</code> and <code>after</code>,
  3212. * specify the number of characters to be used for the integer part
  3213. * and decimal part of the result respectively. Exponential notation
  3214. * is not used. If either parameter is -1 (which indicates the default
  3215. * action), the number of characters used will be exactly as many as
  3216. * are needed for that part.
  3217. * <p>
  3218. * <code>before</code> must be a positive number; if it is larger than
  3219. * is needed to contain the integer part, that part is padded on the
  3220. * left with blanks to the requested length. If <code>before</code> is
  3221. * not large enough to contain the integer part of the number
  3222. * (including the sign, for negative numbers) an exception is thrown.
  3223. * <p>
  3224. * <code>after</code> must be a non-negative number; if it is not the
  3225. * same size as the decimal part of the number, the number will be
  3226. * rounded (or extended with zeros) to fit. Specifying 0 for
  3227. * <code>after</code> will cause the number to be rounded to an
  3228. * integer (that is, it will have no decimal part or decimal point).
  3229. * The rounding method will be the default,
  3230. * <code>MathContext.ROUND_HALF_UP</code>.
  3231. * <p>
  3232. * Other rounding methods, and the use of exponential notation, can
  3233. * be selected by using {@link #format(int,int,int,int,int,int)}.
  3234. * Using the two-parameter form of the method has exactly the same
  3235. * effect as using the six-parameter form with the final four
  3236. * parameters all being -1.
  3237. *
  3238. * @param before The <code>int</code> specifying the number of places
  3239. * before the decimal point. Use -1 for 'as many as
  3240. * are needed'.
  3241. * @param after The <code>int</code> specifying the number of places
  3242. * after the decimal point. Use -1 for 'as many as are
  3243. * needed'.
  3244. * @return A <code>String</code> representing this
  3245. * <code>BigDecimal</code>, laid out according to the
  3246. * specified parameters
  3247. * @throws ArithmeticException if the number cannot be laid out as
  3248. * requested.
  3249. * @throws IllegalArgumentException if a parameter is out of range.
  3250. * @stable ICU 2.0
  3251. * @see #toString
  3252. * @see #toCharArray
  3253. */
  3254. //--public java.lang.String format(int before,int after){
  3255. //-- return format(before,after,-1,-1,com.ibm.icu.math.MathContext.SCIENTIFIC,ROUND_HALF_UP);
  3256. //-- }
  3257. /**
  3258. * Returns the <code>String</code> representation of this
  3259. * <code>BigDecimal</code>, modified by layout parameters and allowing
  3260. * exponential notation.
  3261. * <p>
  3262. * <i>This method is provided as a primitive for use by more
  3263. * sophisticated classes, such as <code>DecimalFormat</code>, that
  3264. * can apply locale-sensitive editing of the result. The level of
  3265. * formatting that it provides is a necessary part of the BigDecimal
  3266. * class as it is sensitive to and must follow the calculation and
  3267. * rounding rules for BigDecimal arithmetic.
  3268. * However, if the function is provided elsewhere, it may be removed
  3269. * from this class. </i>
  3270. * <p>
  3271. * The parameters are all of type <code>int</code>.
  3272. * A value of -1 for any parameter indicates that the default action
  3273. * or value for that parameter should be used.
  3274. * <p>
  3275. * The first two parameters (<code>before</code> and
  3276. * <code>after</code>) specify the number of characters to be used for
  3277. * the integer part and decimal part of the result respectively, as
  3278. * defined for {@link #format(int,int)}.
  3279. * If either of these is -1 (which indicates the default action), the
  3280. * number of characters used will be exactly as many as are needed for
  3281. * that part.
  3282. * <p>
  3283. * The remaining parameters control the use of exponential notation
  3284. * and rounding. Three (<code>explaces</code>, <code>exdigits</code>,
  3285. * and <code>exform</code>) control the exponent part of the result.
  3286. * As before, the default action for any of these parameters may be
  3287. * selected by using the value -1.
  3288. * <p>
  3289. * <code>explaces</code> must be a positive number; it sets the number
  3290. * of places (digits after the sign of the exponent) to be used for
  3291. * any exponent part, the default (when <code>explaces</code> is -1)
  3292. * being to use as many as are needed.
  3293. * If <code>explaces</code> is not -1, space is always reserved for
  3294. * an exponent; if one is not needed (for example, if the exponent
  3295. * will be 0) then <code>explaces</code>+2 blanks are appended to the
  3296. * result.
  3297. * <!-- (This preserves vertical alignment of similarly formatted
  3298. * numbers in a monospace font.) -->
  3299. * If <code>explaces</code> is not -1 and is not large enough to
  3300. * contain the exponent, an exception is thrown.
  3301. * <p>
  3302. * <code>exdigits</code> sets the trigger point for use of exponential
  3303. * notation. If, before any rounding, the number of places needed
  3304. * before the decimal point exceeds <code>exdigits</code>, or if the
  3305. * absolute value of the result is less than <code>0.000001</code>,
  3306. * then exponential form will be used, provided that
  3307. * <code>exdigits</code> was specified.
  3308. * When <code>exdigits</code> is -1, exponential notation will never
  3309. * be used. If 0 is specified for <code>exdigits</code>, exponential
  3310. * notation is always used unless the exponent would be 0.
  3311. * <p>
  3312. * <code>exform</code> sets the form for exponential notation (if
  3313. * needed).
  3314. * It may be either {@link MathContext#SCIENTIFIC} or
  3315. * {@link MathContext#ENGINEERING}.
  3316. * If the latter, engineering, form is requested, up to three digits
  3317. * (plus sign, if negative) may be needed for the integer part of the
  3318. * result (<code>before</code>). Otherwise, only one digit (plus
  3319. * sign, if negative) is needed.
  3320. * <p>
  3321. * Finally, the sixth argument, <code>exround</code>, selects the
  3322. * rounding algorithm to be used, and must be one of the values
  3323. * indicated by a public constant in the {@link MathContext} class
  3324. * whose name starts with <code>ROUND_</code>.
  3325. * The default (<code>ROUND_HALF_UP</code>) may also be selected by
  3326. * using the value -1, as before.
  3327. * <p>
  3328. * The special value <code>MathContext.ROUND_UNNECESSARY</code> may be
  3329. * used to detect whether non-zero digits are discarded -- if
  3330. * <code>exround</code> has this value than if non-zero digits would
  3331. * be discarded (rounded) during formatting then an
  3332. * <code>ArithmeticException</code> is thrown.
  3333. *
  3334. * @param before The <code>int</code> specifying the number of places
  3335. * before the decimal point.
  3336. * Use -1 for 'as many as are needed'.
  3337. * @param after The <code>int</code> specifying the number of places
  3338. * after the decimal point.
  3339. * Use -1 for 'as many as are needed'.
  3340. * @param explaces The <code>int</code> specifying the number of places
  3341. * to be used for any exponent.
  3342. * Use -1 for 'as many as are needed'.
  3343. * @param exdigits The <code>int</code> specifying the trigger
  3344. * (digits before the decimal point) which if
  3345. * exceeded causes exponential notation to be used.
  3346. * Use 0 to force exponential notation.
  3347. * Use -1 to force plain notation (no exponential
  3348. * notation).
  3349. * @param exform The <code>int</code> specifying the form of
  3350. * exponential notation to be used
  3351. * ({@link MathContext#SCIENTIFIC} or
  3352. * {@link MathContext#ENGINEERING}).
  3353. * @param exround The <code>int</code> specifying the rounding mode
  3354. * to use.
  3355. * Use -1 for the default, {@link MathContext#ROUND_HALF_UP}.
  3356. * @return A <code>String</code> representing this
  3357. * <code>BigDecimal</code>, laid out according to the
  3358. * specified parameters
  3359. * @throws ArithmeticException if the number cannot be laid out as
  3360. * requested.
  3361. * @throws IllegalArgumentException if a parameter is out of range.
  3362. * @see #toString
  3363. * @see #toCharArray
  3364. * @stable ICU 2.0
  3365. */
  3366. //--public java.lang.String format(int before,int after,int explaces,int exdigits,int exformint,int exround){
  3367. function format() {
  3368. var explaces;
  3369. var exdigits;
  3370. var exformint;
  3371. var exround;
  3372. if (format.arguments.length == 6)
  3373. {
  3374. explaces = format.arguments[2];
  3375. exdigits = format.arguments[3];
  3376. exformint = format.arguments[4];
  3377. exround = format.arguments[5];
  3378. }
  3379. else if (format.arguments.length == 2)
  3380. {
  3381. explaces = -1;
  3382. exdigits = -1;
  3383. exformint = MathContext.prototype.SCIENTIFIC;
  3384. exround = this.ROUND_HALF_UP;
  3385. }
  3386. else
  3387. {
  3388. throw "format(): " + format.arguments.length + " arguments given; expected 2 or 6";
  3389. }
  3390. var before = format.arguments[0];
  3391. var after = format.arguments[1];
  3392. //--com.ibm.icu.math.BigDecimal num;
  3393. var num;
  3394. //--int mag=0;
  3395. var mag=0;
  3396. //--int thisafter=0;
  3397. var thisafter=0;
  3398. //--int lead=0;
  3399. var lead=0;
  3400. //--byte newmant[]=null;
  3401. var newmant=null;
  3402. //--int chop=0;
  3403. var chop=0;
  3404. //--int need=0;
  3405. var need=0;
  3406. //--int oldexp=0;
  3407. var oldexp=0;
  3408. //--char a[];
  3409. var a;
  3410. //--int p=0;
  3411. var p=0;
  3412. //--char newa[]=null;
  3413. var newa=null;
  3414. //--int i=0;
  3415. var i=0;
  3416. //--int places=0;
  3417. var places=0;
  3418. /* Check arguments */
  3419. if ((before<(-1))||(before==0))
  3420. this.badarg("format",1,before);
  3421. if (after<(-1))
  3422. this.badarg("format",2,after);
  3423. if ((explaces<(-1))||(explaces==0))
  3424. this.badarg("format",3,explaces);
  3425. if (exdigits<(-1))
  3426. this.badarg("format",4,exdigits);
  3427. {/*select*/
  3428. if (exformint==MathContext.prototype.SCIENTIFIC)
  3429. {}
  3430. else if (exformint==MathContext.prototype.ENGINEERING)
  3431. {}
  3432. else if (exformint==(-1))
  3433. exformint=MathContext.prototype.SCIENTIFIC;
  3434. // note PLAIN isn't allowed
  3435. else{
  3436. this.badarg("format",5,exformint);
  3437. }
  3438. }
  3439. // checking the rounding mode is done by trying to construct a
  3440. // MathContext object with that mode; it will fail if bad
  3441. if (exround!=this.ROUND_HALF_UP)
  3442. {try{ // if non-default...
  3443. if (exround==(-1))
  3444. exround=this.ROUND_HALF_UP;
  3445. else
  3446. new MathContext(9,MathContext.prototype.SCIENTIFIC,false,exround);
  3447. }
  3448. catch ($10){
  3449. this.badarg("format",6,exround);
  3450. }}
  3451. num=this.clone(this); // make private copy
  3452. /* Here:
  3453. num is BigDecimal to format
  3454. before is places before point [>0]
  3455. after is places after point [>=0]
  3456. explaces is exponent places [>0]
  3457. exdigits is exponent digits [>=0]
  3458. exformint is exponent form [one of two]
  3459. exround is rounding mode [one of eight]
  3460. 'before' through 'exdigits' are -1 if not specified
  3461. */
  3462. /* determine form */
  3463. {setform:do{/*select*/
  3464. if (exdigits==(-1))
  3465. num.form=MathContext.prototype.PLAIN;
  3466. else if (num.ind==this.iszero)
  3467. num.form=MathContext.prototype.PLAIN;
  3468. else{
  3469. // determine whether triggers
  3470. mag=num.exp+num.mant.length;
  3471. if (mag>exdigits)
  3472. num.form=exformint;
  3473. else
  3474. if (mag<(-5))
  3475. num.form=exformint;
  3476. else
  3477. num.form=MathContext.prototype.PLAIN;
  3478. }
  3479. }while(false);}/*setform*/
  3480. /* If 'after' was specified then we may need to adjust the
  3481. mantissa. This is a little tricky, as we must conform to the
  3482. rules of exponential layout if necessary (e.g., we cannot end up
  3483. with 10.0 if scientific). */
  3484. if (after>=0)
  3485. {setafter:for(;;){
  3486. // calculate the current after-length
  3487. {/*select*/
  3488. if (num.form==MathContext.prototype.PLAIN)
  3489. thisafter=-num.exp; // has decimal part
  3490. else if (num.form==MathContext.prototype.SCIENTIFIC)
  3491. thisafter=num.mant.length-1;
  3492. else{ // engineering
  3493. lead=(((num.exp+num.mant.length)-1))%3; // exponent to use
  3494. if (lead<0)
  3495. lead=3+lead; // negative exponent case
  3496. lead++; // number of leading digits
  3497. if (lead>=num.mant.length)
  3498. thisafter=0;
  3499. else
  3500. thisafter=num.mant.length-lead;
  3501. }
  3502. }
  3503. if (thisafter==after)
  3504. break setafter; // we're in luck
  3505. if (thisafter<after)
  3506. { // need added trailing zeros
  3507. // [thisafter can be negative]
  3508. newmant=this.extend(num.mant,(num.mant.length+after)-thisafter);
  3509. num.mant=newmant;
  3510. num.exp=num.exp-((after-thisafter)); // adjust exponent
  3511. if (num.exp<this.MinExp)
  3512. throw "format(): Exponent Overflow: " + num.exp;
  3513. break setafter;
  3514. }
  3515. // We have too many digits after the decimal point; this could
  3516. // cause a carry, which could change the mantissa...
  3517. // Watch out for implied leading zeros in PLAIN case
  3518. chop=thisafter-after; // digits to lop [is >0]
  3519. if (chop>num.mant.length)
  3520. { // all digits go, no chance of carry
  3521. // carry on with zero
  3522. num.mant=this.ZERO.mant;
  3523. num.ind=this.iszero;
  3524. num.exp=0;
  3525. continue setafter; // recheck: we may need trailing zeros
  3526. }
  3527. // we have a digit to inspect from existing mantissa
  3528. // round the number as required
  3529. need=num.mant.length-chop; // digits to end up with [may be 0]
  3530. oldexp=num.exp; // save old exponent
  3531. num.round(need,exround);
  3532. // if the exponent grew by more than the digits we chopped, then
  3533. // we must have had a carry, so will need to recheck the layout
  3534. if ((num.exp-oldexp)==chop)
  3535. break setafter; // number did not have carry
  3536. // mantissa got extended .. so go around and check again
  3537. }
  3538. }/*setafter*/
  3539. a=num.layout(); // lay out, with exponent if required, etc.
  3540. /* Here we have laid-out number in 'a' */
  3541. // now apply 'before' and 'explaces' as needed
  3542. if (before>0)
  3543. {
  3544. // look for '.' or 'E'
  3545. {var $11=a.length;p=0;p:for(;$11>0;$11--,p++){
  3546. if (a[p]=='.')
  3547. break p;
  3548. if (a[p]=='E')
  3549. break p;
  3550. }
  3551. }/*p*/
  3552. // p is now offset of '.', 'E', or character after end of array
  3553. // that is, the current length of before part
  3554. if (p>before)
  3555. this.badarg("format",1,before); // won't fit
  3556. if (p<before)
  3557. { // need leading blanks
  3558. newa=new Array((a.length+before)-p);
  3559. {var $12=before-p;i=0;i:for(;$12>0;$12--,i++){
  3560. newa[i]=' ';
  3561. }
  3562. }/*i*/
  3563. //--java.lang.System.arraycopy((java.lang.Object)a,0,(java.lang.Object)newa,i,a.length);
  3564. this.arraycopy(a,0,newa,i,a.length);
  3565. a=newa;
  3566. }
  3567. // [if p=before then it's just the right length]
  3568. }
  3569. if (explaces>0)
  3570. {
  3571. // look for 'E' [cannot be at offset 0]
  3572. {var $13=a.length-1;p=a.length-1;p:for(;$13>0;$13--,p--){
  3573. if (a[p]=='E')
  3574. break p;
  3575. }
  3576. }/*p*/
  3577. // p is now offset of 'E', or 0
  3578. if (p==0)
  3579. { // no E part; add trailing blanks
  3580. newa=new Array((a.length+explaces)+2);
  3581. //--java.lang.System.arraycopy((java.lang.Object)a,0,(java.lang.Object)newa,0,a.length);
  3582. this.arraycopy(a,0,newa,0,a.length);
  3583. {var $14=explaces+2;i=a.length;i:for(;$14>0;$14--,i++){
  3584. newa[i]=' ';
  3585. }
  3586. }/*i*/
  3587. a=newa;
  3588. }
  3589. else
  3590. {/* found E */ // may need to insert zeros
  3591. places=(a.length-p)-2; // number so far
  3592. if (places>explaces)
  3593. this.badarg("format",3,explaces);
  3594. if (places<explaces)
  3595. { // need to insert zeros
  3596. newa=new Array((a.length+explaces)-places);
  3597. //--java.lang.System.arraycopy((java.lang.Object)a,0,(java.lang.Object)newa,0,p+2); // through E and sign
  3598. this.arraycopy(a,0,newa,0,p+2);
  3599. {var $15=explaces-places;i=p+2;i:for(;$15>0;$15--,i++){
  3600. newa[i]='0';
  3601. }
  3602. }/*i*/
  3603. //--java.lang.System.arraycopy((java.lang.Object)a,p+2,(java.lang.Object)newa,i,places); // remainder of exponent
  3604. this.arraycopy(a,p+2,newa,i,places);
  3605. a=newa;
  3606. }
  3607. // [if places=explaces then it's just the right length]
  3608. }
  3609. }
  3610. return a.join("");
  3611. }
  3612. /**
  3613. * Returns the hashcode for this <code>BigDecimal</code>.
  3614. * This hashcode is suitable for use by the
  3615. * <code>java.util.Hashtable</code> class.
  3616. * <p>
  3617. * Note that two <code>BigDecimal</code> objects are only guaranteed
  3618. * to produce the same hashcode if they are exactly equal (that is,
  3619. * the <code>String</code> representations of the
  3620. * <code>BigDecimal</code> numbers are identical -- they have the same
  3621. * characters in the same sequence).
  3622. *
  3623. * @return An <code>int</code> that is the hashcode for <code>this</code>.
  3624. * @stable ICU 2.0
  3625. */
  3626. //--public int hashCode(){
  3627. //-- // Maybe calculate ourselves, later. If so, note that there can be
  3628. //-- // more than one internal representation for a given toString() result.
  3629. //-- return this.toString().hashCode();
  3630. //-- }
  3631. /**
  3632. * Converts this <code>BigDecimal</code> to an <code>int</code>.
  3633. * If the <code>BigDecimal</code> has a non-zero decimal part it is
  3634. * discarded. If the <code>BigDecimal</code> is out of the possible
  3635. * range for an <code>int</code> (32-bit signed integer) result then
  3636. * only the low-order 32 bits are used. (That is, the number may be
  3637. * <i>decapitated</i>.) To avoid unexpected errors when these
  3638. * conditions occur, use the {@link #intValueExact} method.
  3639. *
  3640. * @return An <code>int</code> converted from <code>this</code>,
  3641. * truncated and decapitated if necessary.
  3642. * @stable ICU 2.0
  3643. */
  3644. //--public int intValue(){
  3645. //-- return toBigInteger().intValue();
  3646. //-- }
  3647. /**
  3648. * Converts this <code>BigDecimal</code> to an <code>int</code>.
  3649. * If the <code>BigDecimal</code> has a non-zero decimal part or is
  3650. * out of the possible range for an <code>int</code> (32-bit signed
  3651. * integer) result then an <code>ArithmeticException</code> is thrown.
  3652. *
  3653. * @return An <code>int</code> equal in value to <code>this</code>.
  3654. * @throws ArithmeticException if <code>this</code> has a non-zero
  3655. * decimal part, or will not fit in an
  3656. * <code>int</code>.
  3657. * @stable ICU 2.0
  3658. */
  3659. //--public int intValueExact(){
  3660. function intValueExact() {
  3661. //--int lodigit;
  3662. var lodigit;
  3663. //--int useexp=0;
  3664. var useexp=0;
  3665. //--int result;
  3666. var result;
  3667. //--int i=0;
  3668. var i=0;
  3669. //--int topdig=0;
  3670. var topdig=0;
  3671. // This does not use longValueExact() as the latter can be much
  3672. // slower.
  3673. // intcheck (from pow) relies on this to check decimal part
  3674. if (this.ind==this.iszero)
  3675. return 0; // easy, and quite common
  3676. /* test and drop any trailing decimal part */
  3677. lodigit=this.mant.length-1;
  3678. if (this.exp<0)
  3679. {
  3680. lodigit=lodigit+this.exp; // reduces by -(-exp)
  3681. /* all decimal places must be 0 */
  3682. if ((!(this.allzero(this.mant,lodigit+1))))
  3683. throw "intValueExact(): Decimal part non-zero: " + this.toString();
  3684. if (lodigit<0)
  3685. return 0; // -1<this<1
  3686. useexp=0;
  3687. }
  3688. else
  3689. {/* >=0 */
  3690. if ((this.exp+lodigit)>9) // early exit
  3691. throw "intValueExact(): Conversion overflow: "+this.toString();
  3692. useexp=this.exp;
  3693. }
  3694. /* convert the mantissa to binary, inline for speed */
  3695. result=0;
  3696. {var $16=lodigit+useexp;i=0;i:for(;i<=$16;i++){
  3697. result=result*10;
  3698. if (i<=lodigit)
  3699. result=result+this.mant[i];
  3700. }
  3701. }/*i*/
  3702. /* Now, if the risky length, check for overflow */
  3703. if ((lodigit+useexp)==9)
  3704. {
  3705. // note we cannot just test for -ve result, as overflow can move a
  3706. // zero into the top bit [consider 5555555555]
  3707. topdig=div(result,1000000000); // get top digit, preserving sign
  3708. if (topdig!=this.mant[0])
  3709. { // digit must match and be positive
  3710. // except in the special case ...
  3711. if (result==-2147483648) // looks like the special
  3712. if (this.ind==this.isneg) // really was negative
  3713. if (this.mant[0]==2)
  3714. return result; // really had top digit 2
  3715. throw "intValueExact(): Conversion overflow: "+this.toString();
  3716. }
  3717. }
  3718. /* Looks good */
  3719. if (this.ind==this.ispos)
  3720. return result;
  3721. return -result;
  3722. }
  3723. /**
  3724. * Converts this <code>BigDecimal</code> to a <code>long</code>.
  3725. * If the <code>BigDecimal</code> has a non-zero decimal part it is
  3726. * discarded. If the <code>BigDecimal</code> is out of the possible
  3727. * range for a <code>long</code> (64-bit signed integer) result then
  3728. * only the low-order 64 bits are used. (That is, the number may be
  3729. * <i>decapitated</i>.) To avoid unexpected errors when these
  3730. * conditions occur, use the {@link #longValueExact} method.
  3731. *
  3732. * @return A <code>long</code> converted from <code>this</code>,
  3733. * truncated and decapitated if necessary.
  3734. * @stable ICU 2.0
  3735. */
  3736. //--public long longValue(){
  3737. //-- return toBigInteger().longValue();
  3738. //-- }
  3739. /**
  3740. * Converts this <code>BigDecimal</code> to a <code>long</code>.
  3741. * If the <code>BigDecimal</code> has a non-zero decimal part or is
  3742. * out of the possible range for a <code>long</code> (64-bit signed
  3743. * integer) result then an <code>ArithmeticException</code> is thrown.
  3744. *
  3745. * @return A <code>long</code> equal in value to <code>this</code>.
  3746. * @throws ArithmeticException if <code>this</code> has a non-zero
  3747. * decimal part, or will not fit in a
  3748. * <code>long</code>.
  3749. * @stable ICU 2.0
  3750. */
  3751. //--public long longValueExact(){
  3752. //-- int lodigit;
  3753. //-- int cstart=0;
  3754. //-- int useexp=0;
  3755. //-- long result;
  3756. //-- int i=0;
  3757. //-- long topdig=0;
  3758. //-- // Identical to intValueExact except for result=long, and exp>=20 test
  3759. //-- if (ind==0)
  3760. //-- return 0; // easy, and quite common
  3761. //-- lodigit=mant.length-1; // last included digit
  3762. //-- if (exp<0)
  3763. //-- {
  3764. //-- lodigit=lodigit+exp; // -(-exp)
  3765. //-- /* all decimal places must be 0 */
  3766. //-- if (lodigit<0)
  3767. //-- cstart=0;
  3768. //-- else
  3769. //-- cstart=lodigit+1;
  3770. //-- if ((!(allzero(mant,cstart))))
  3771. //-- throw new java.lang.ArithmeticException("Decimal part non-zero:"+" "+this.toString());
  3772. //-- if (lodigit<0)
  3773. //-- return 0; // -1<this<1
  3774. //-- useexp=0;
  3775. //-- }
  3776. //-- else
  3777. //-- {/* >=0 */
  3778. //-- if ((exp+mant.length)>18) // early exit
  3779. //-- throw new java.lang.ArithmeticException("Conversion overflow:"+" "+this.toString());
  3780. //-- useexp=exp;
  3781. //-- }
  3782. //--
  3783. //-- /* convert the mantissa to binary, inline for speed */
  3784. //-- // note that we could safely use the 'test for wrap to negative'
  3785. //-- // algorithm here, but instead we parallel the intValueExact
  3786. //-- // algorithm for ease of checking and maintenance.
  3787. //-- result=(long)0;
  3788. //-- {int $17=lodigit+useexp;i=0;i:for(;i<=$17;i++){
  3789. //-- result=result*10;
  3790. //-- if (i<=lodigit)
  3791. //-- result=result+mant[i];
  3792. //-- }
  3793. //-- }/*i*/
  3794. //--
  3795. //-- /* Now, if the risky length, check for overflow */
  3796. //-- if ((lodigit+useexp)==18)
  3797. //-- {
  3798. //-- topdig=result/1000000000000000000L; // get top digit, preserving sign
  3799. //-- if (topdig!=mant[0])
  3800. //-- { // digit must match and be positive
  3801. //-- // except in the special case ...
  3802. //-- if (result==java.lang.Long.MIN_VALUE) // looks like the special
  3803. //-- if (ind==isneg) // really was negative
  3804. //-- if (mant[0]==9)
  3805. //-- return result; // really had top digit 9
  3806. //-- throw new java.lang.ArithmeticException("Conversion overflow:"+" "+this.toString());
  3807. //-- }
  3808. //-- }
  3809. //--
  3810. //-- /* Looks good */
  3811. //-- if (ind==ispos)
  3812. //-- return result;
  3813. //-- return (long)-result;
  3814. //-- }
  3815. /**
  3816. * Returns a plain <code>BigDecimal</code> whose decimal point has
  3817. * been moved to the left by a specified number of positions.
  3818. * The parameter, <code>n</code>, specifies the number of positions to
  3819. * move the decimal point.
  3820. * That is, if <code>n</code> is 0 or positive, the number returned is
  3821. * given by:
  3822. * <p><code>
  3823. * this.multiply(TEN.pow(new BigDecimal(-n)))
  3824. * </code>
  3825. * <p>
  3826. * <code>n</code> may be negative, in which case the method returns
  3827. * the same result as <code>movePointRight(-n)</code>.
  3828. *
  3829. * @param n The <code>int</code> specifying the number of places to
  3830. * move the decimal point leftwards.
  3831. * @return A <code>BigDecimal</code> derived from
  3832. * <code>this</code>, with the decimal point moved
  3833. * <code>n</code> places to the left.
  3834. * @stable ICU 2.0
  3835. */
  3836. //--public com.ibm.icu.math.BigDecimal movePointLeft(int n){
  3837. function movePointLeft(n) {
  3838. //--com.ibm.icu.math.BigDecimal res;
  3839. var res;
  3840. // very little point in optimizing for shift of 0
  3841. res=this.clone(this);
  3842. res.exp=res.exp-n;
  3843. return res.finish(this.plainMC,false); // finish sets form and checks exponent
  3844. }
  3845. /**
  3846. * Returns a plain <code>BigDecimal</code> whose decimal point has
  3847. * been moved to the right by a specified number of positions.
  3848. * The parameter, <code>n</code>, specifies the number of positions to
  3849. * move the decimal point.
  3850. * That is, if <code>n</code> is 0 or positive, the number returned is
  3851. * given by:
  3852. * <p><code>
  3853. * this.multiply(TEN.pow(new BigDecimal(n)))
  3854. * </code>
  3855. * <p>
  3856. * <code>n</code> may be negative, in which case the method returns
  3857. * the same result as <code>movePointLeft(-n)</code>.
  3858. *
  3859. * @param n The <code>int</code> specifying the number of places to
  3860. * move the decimal point rightwards.
  3861. * @return A <code>BigDecimal</code> derived from
  3862. * <code>this</code>, with the decimal point moved
  3863. * <code>n</code> places to the right.
  3864. * @stable ICU 2.0
  3865. */
  3866. //--public com.ibm.icu.math.BigDecimal movePointRight(int n){
  3867. function movePointRight(n) {
  3868. //--com.ibm.icu.math.BigDecimal res;
  3869. var res;
  3870. res=this.clone(this);
  3871. res.exp=res.exp+n;
  3872. return res.finish(this.plainMC,false);
  3873. }
  3874. /**
  3875. * Returns the scale of this <code>BigDecimal</code>.
  3876. * Returns a non-negative <code>int</code> which is the scale of the
  3877. * number. The scale is the number of digits in the decimal part of
  3878. * the number if the number were formatted without exponential
  3879. * notation.
  3880. *
  3881. * @return An <code>int</code> whose value is the scale of this
  3882. * <code>BigDecimal</code>.
  3883. * @stable ICU 2.0
  3884. */
  3885. //--public int scale(){
  3886. function scale() {
  3887. if (this.exp>=0)
  3888. return 0; // scale can never be negative
  3889. return -this.exp;
  3890. }
  3891. /**
  3892. * Returns a plain <code>BigDecimal</code> with a given scale.
  3893. * <p>
  3894. * If the given scale (which must be zero or positive) is the same as
  3895. * or greater than the length of the decimal part (the scale) of this
  3896. * <code>BigDecimal</code> then trailing zeros will be added to the
  3897. * decimal part as necessary.
  3898. * <p>
  3899. * If the given scale is less than the length of the decimal part (the
  3900. * scale) of this <code>BigDecimal</code> then trailing digits
  3901. * will be removed, and in this case an
  3902. * <code>ArithmeticException</code> is thrown if any discarded digits
  3903. * are non-zero.
  3904. * <p>
  3905. * The same as {@link #setScale(int, int)}, where the first parameter
  3906. * is the scale, and the second is
  3907. * <code>MathContext.ROUND_UNNECESSARY</code>.
  3908. *
  3909. * @param scale The <code>int</code> specifying the scale of the
  3910. * resulting <code>BigDecimal</code>.
  3911. * @return A plain <code>BigDecimal</code> with the given scale.
  3912. * @throws ArithmeticException if <code>scale</code> is negative.
  3913. * @throws ArithmeticException if reducing scale would discard
  3914. * non-zero digits.
  3915. * @stable ICU 2.0
  3916. */
  3917. //--public com.ibm.icu.math.BigDecimal setScale(int scale){
  3918. //-- return setScale(scale,ROUND_UNNECESSARY);
  3919. //-- }
  3920. /**
  3921. * Returns a plain <code>BigDecimal</code> with a given scale.
  3922. * <p>
  3923. * If the given scale (which must be zero or positive) is the same as
  3924. * or greater than the length of the decimal part (the scale) of this
  3925. * <code>BigDecimal</code> then trailing zeros will be added to the
  3926. * decimal part as necessary.
  3927. * <p>
  3928. * If the given scale is less than the length of the decimal part (the
  3929. * scale) of this <code>BigDecimal</code> then trailing digits
  3930. * will be removed, and the rounding mode given by the second
  3931. * parameter is used to determine if the remaining digits are
  3932. * affected by a carry.
  3933. * In this case, an <code>IllegalArgumentException</code> is thrown if
  3934. * <code>round</code> is not a valid rounding mode.
  3935. * <p>
  3936. * If <code>round</code> is <code>MathContext.ROUND_UNNECESSARY</code>,
  3937. * an <code>ArithmeticException</code> is thrown if any discarded
  3938. * digits are non-zero.
  3939. *
  3940. * @param scale The <code>int</code> specifying the scale of the
  3941. * resulting <code>BigDecimal</code>.
  3942. * @param round The <code>int</code> rounding mode to be used for
  3943. * the division (see the {@link MathContext} class).
  3944. * @return A plain <code>BigDecimal</code> with the given scale.
  3945. * @throws IllegalArgumentException if <code>round</code> is not a
  3946. * valid rounding mode.
  3947. * @throws ArithmeticException if <code>scale</code> is negative.
  3948. * @throws ArithmeticException if <code>round</code> is
  3949. * <code>MathContext.ROUND_UNNECESSARY</code>, and
  3950. * reducing scale would discard non-zero digits.
  3951. * @stable ICU 2.0
  3952. */
  3953. //--public com.ibm.icu.math.BigDecimal setScale(int scale,int round){
  3954. function setScale() {
  3955. var round;
  3956. if (setScale.arguments.length == 2)
  3957. {
  3958. round = setScale.arguments[1];
  3959. }
  3960. else if (setScale.arguments.length == 1)
  3961. {
  3962. round = this.ROUND_UNNECESSARY;
  3963. }
  3964. else
  3965. {
  3966. throw "setScale(): " + setScale.arguments.length + " given; expected 1 or 2";
  3967. }
  3968. var scale = setScale.arguments[0];
  3969. //--int ourscale;
  3970. var ourscale;
  3971. //--com.ibm.icu.math.BigDecimal res;
  3972. var res;
  3973. //--int padding=0;
  3974. var padding=0;
  3975. //--int newlen=0;
  3976. var newlen=0;
  3977. // at present this naughtily only checks the round value if it is
  3978. // needed (used), for speed
  3979. ourscale=this.scale();
  3980. if (ourscale==scale) // already correct scale
  3981. if (this.form==MathContext.prototype.PLAIN) // .. and form
  3982. return this;
  3983. res=this.clone(this); // need copy
  3984. if (ourscale<=scale)
  3985. { // simply zero-padding/changing form
  3986. // if ourscale is 0 we may have lots of 0s to add
  3987. if (ourscale==0)
  3988. padding=res.exp+scale;
  3989. else
  3990. padding=scale-ourscale;
  3991. res.mant=this.extend(res.mant,res.mant.length+padding);
  3992. res.exp=-scale; // as requested
  3993. }
  3994. else
  3995. {/* ourscale>scale: shortening, probably */
  3996. if (scale<0)
  3997. //--throw new java.lang.ArithmeticException("Negative scale:"+" "+scale);
  3998. throw "setScale(): Negative scale: " + scale;
  3999. // [round() will raise exception if invalid round]
  4000. newlen=res.mant.length-((ourscale-scale)); // [<=0 is OK]
  4001. res=res.round(newlen,round); // round to required length
  4002. // This could have shifted left if round (say) 0.9->1[.0]
  4003. // Repair if so by adding a zero and reducing exponent
  4004. if (res.exp!=(-scale))
  4005. {
  4006. res.mant=this.extend(res.mant,res.mant.length+1);
  4007. res.exp=res.exp-1;
  4008. }
  4009. }
  4010. res.form=MathContext.prototype.PLAIN; // by definition
  4011. return res;
  4012. }
  4013. /**
  4014. * Converts this <code>BigDecimal</code> to a <code>short</code>.
  4015. * If the <code>BigDecimal</code> has a non-zero decimal part or is
  4016. * out of the possible range for a <code>short</code> (16-bit signed
  4017. * integer) result then an <code>ArithmeticException</code> is thrown.
  4018. *
  4019. * @return A <code>short</code> equal in value to <code>this</code>.
  4020. * @throws ArithmeticException if <code>this</code> has a non-zero
  4021. * decimal part, or will not fit in a
  4022. * <code>short</code>.
  4023. * @stable ICU 2.0
  4024. */
  4025. //--public short shortValueExact(){
  4026. //-- int num;
  4027. //-- num=this.intValueExact(); // will check decimal part too
  4028. //-- if ((num>32767)|(num<(-32768)))
  4029. //-- throw new java.lang.ArithmeticException("Conversion overflow:"+" "+this.toString());
  4030. //-- return (short)num;
  4031. //-- }
  4032. /**
  4033. * Returns the sign of this <code>BigDecimal</code>, as an
  4034. * <code>int</code>.
  4035. * This returns the <i>signum</i> function value that represents the
  4036. * sign of this <code>BigDecimal</code>.
  4037. * That is, -1 if the <code>BigDecimal</code> is negative, 0 if it is
  4038. * numerically equal to zero, or 1 if it is positive.
  4039. *
  4040. * @return An <code>int</code> which is -1 if the
  4041. * <code>BigDecimal</code> is negative, 0 if it is
  4042. * numerically equal to zero, or 1 if it is positive.
  4043. * @stable ICU 2.0
  4044. */
  4045. //--public int signum(){
  4046. function signum() {
  4047. return this.ind; // [note this assumes values for ind.]
  4048. }
  4049. /**
  4050. * Converts this <code>BigDecimal</code> to a
  4051. * <code>java.math.BigDecimal</code>.
  4052. * <p>
  4053. * This is an exact conversion; the result is the same as if the
  4054. * <code>BigDecimal</code> were formatted as a plain number without
  4055. * any rounding or exponent and then the
  4056. * <code>java.math.BigDecimal(java.lang.String)</code> constructor
  4057. * were used to construct the result.
  4058. * <p>
  4059. * <i>(Note: this method is provided only in the
  4060. * <code>com.ibm.icu.math</code> version of the BigDecimal class.
  4061. * It would not be present in a <code>java.math</code> version.)</i>
  4062. *
  4063. * @return The <code>java.math.BigDecimal</code> equal in value
  4064. * to this <code>BigDecimal</code>.
  4065. * @stable ICU 2.0
  4066. */
  4067. //--public java.math.BigDecimal toBigDecimal(){
  4068. //-- return new java.math.BigDecimal(this.unscaledValue(),this.scale());
  4069. //-- }
  4070. /**
  4071. * Converts this <code>BigDecimal</code> to a
  4072. * <code>java.math.BigInteger</code>.
  4073. * <p>
  4074. * Any decimal part is truncated (discarded).
  4075. * If an exception is desired should the decimal part be non-zero,
  4076. * use {@link #toBigIntegerExact()}.
  4077. *
  4078. * @return The <code>java.math.BigInteger</code> equal in value
  4079. * to the integer part of this <code>BigDecimal</code>.
  4080. * @stable ICU 2.0
  4081. */
  4082. //--public java.math.BigInteger toBigInteger(){
  4083. //-- com.ibm.icu.math.BigDecimal res=null;
  4084. //-- int newlen=0;
  4085. //-- byte newmant[]=null;
  4086. //-- {/*select*/
  4087. //-- if ((exp>=0)&(form==com.ibm.icu.math.MathContext.PLAIN))
  4088. //-- res=this; // can layout simply
  4089. //-- else if (exp>=0)
  4090. //-- {
  4091. //-- res=clone(this); // safe copy
  4092. //-- res.form=(byte)com.ibm.icu.math.MathContext.PLAIN; // .. and request PLAIN
  4093. //-- }
  4094. //-- else{
  4095. //-- { // exp<0; scale to be truncated
  4096. //-- // we could use divideInteger, but we may as well be quicker
  4097. //-- if (((int)-this.exp)>=this.mant.length)
  4098. //-- res=ZERO; // all blows away
  4099. //-- else
  4100. //-- {
  4101. //-- res=clone(this); // safe copy
  4102. //-- newlen=res.mant.length+res.exp;
  4103. //-- newmant=new byte[newlen]; // [shorter]
  4104. //-- java.lang.System.arraycopy((java.lang.Object)res.mant,0,(java.lang.Object)newmant,0,newlen);
  4105. //-- res.mant=newmant;
  4106. //-- res.form=(byte)com.ibm.icu.math.MathContext.PLAIN;
  4107. //-- res.exp=0;
  4108. //-- }
  4109. //-- }
  4110. //-- }
  4111. //-- }
  4112. //-- return new BigInteger(new java.lang.String(res.layout()));
  4113. //-- }
  4114. /**
  4115. * Converts this <code>BigDecimal</code> to a
  4116. * <code>java.math.BigInteger</code>.
  4117. * <p>
  4118. * An exception is thrown if the decimal part (if any) is non-zero.
  4119. *
  4120. * @return The <code>java.math.BigInteger</code> equal in value
  4121. * to the integer part of this <code>BigDecimal</code>.
  4122. * @throws ArithmeticException if <code>this</code> has a non-zero
  4123. * decimal part.
  4124. * @stable ICU 2.0
  4125. */
  4126. //--public java.math.BigInteger toBigIntegerExact(){
  4127. //-- /* test any trailing decimal part */
  4128. //-- if (exp<0)
  4129. //-- { // possible decimal part
  4130. //-- /* all decimal places must be 0; note exp<0 */
  4131. //-- if ((!(allzero(mant,mant.length+exp))))
  4132. //-- throw new java.lang.ArithmeticException("Decimal part non-zero:"+" "+this.toString());
  4133. //-- }
  4134. //-- return toBigInteger();
  4135. //-- }
  4136. /**
  4137. * Returns the <code>BigDecimal</code> as a character array.
  4138. * The result of this method is the same as using the
  4139. * sequence <code>toString().toCharArray()</code>, but avoids creating
  4140. * the intermediate <code>String</code> and <code>char[]</code>
  4141. * objects.
  4142. *
  4143. * @return The <code>char[]</code> array corresponding to this
  4144. * <code>BigDecimal</code>.
  4145. * @stable ICU 2.0
  4146. */
  4147. //--public char[] toCharArray(){
  4148. //-- return layout();
  4149. //-- }
  4150. /**
  4151. * Returns the <code>BigDecimal</code> as a <code>String</code>.
  4152. * This returns a <code>String</code> that exactly represents this
  4153. * <code>BigDecimal</code>, as defined in the decimal documentation
  4154. * (see {@link BigDecimal class header}).
  4155. * <p>
  4156. * By definition, using the {@link #BigDecimal(String)} constructor
  4157. * on the result <code>String</code> will create a
  4158. * <code>BigDecimal</code> that is exactly equal to the original
  4159. * <code>BigDecimal</code>.
  4160. *
  4161. * @return The <code>String</code> exactly corresponding to this
  4162. * <code>BigDecimal</code>.
  4163. * @see #format(int, int)
  4164. * @see #format(int, int, int, int, int, int)
  4165. * @see #toCharArray()
  4166. * @stable ICU 2.0
  4167. */
  4168. //--public java.lang.String toString(){
  4169. function toString() {
  4170. return this.layout().join("");
  4171. }
  4172. /**
  4173. * Returns the number as a <code>BigInteger</code> after removing the
  4174. * scale.
  4175. * That is, the number is expressed as a plain number, any decimal
  4176. * point is then removed (retaining the digits of any decimal part),
  4177. * and the result is then converted to a <code>BigInteger</code>.
  4178. *
  4179. * @return The <code>java.math.BigInteger</code> equal in value to
  4180. * this <code>BigDecimal</code> multiplied by ten to the
  4181. * power of <code>this.scale()</code>.
  4182. * @stable ICU 2.0
  4183. */
  4184. //--public java.math.BigInteger unscaledValue(){
  4185. //-- com.ibm.icu.math.BigDecimal res=null;
  4186. //-- if (exp>=0)
  4187. //-- res=this;
  4188. //-- else
  4189. //-- {
  4190. //-- res=clone(this); // safe copy
  4191. //-- res.exp=0; // drop scale
  4192. //-- }
  4193. //-- return res.toBigInteger();
  4194. //-- }
  4195. /**
  4196. * Translates a <code>double</code> to a <code>BigDecimal</code>.
  4197. * <p>
  4198. * Returns a <code>BigDecimal</code> which is the decimal
  4199. * representation of the 64-bit signed binary floating point
  4200. * parameter. If the parameter is infinite, or is not a number (NaN),
  4201. * a <code>NumberFormatException</code> is thrown.
  4202. * <p>
  4203. * The number is constructed as though <code>num</code> had been
  4204. * converted to a <code>String</code> using the
  4205. * <code>Double.toString()</code> method and the
  4206. * {@link #BigDecimal(java.lang.String)} constructor had then been used.
  4207. * This is typically not an exact conversion.
  4208. *
  4209. * @param dub The <code>double</code> to be translated.
  4210. * @return The <code>BigDecimal</code> equal in value to
  4211. * <code>dub</code>.
  4212. * @throws NumberFormatException if the parameter is infinite or
  4213. * not a number.
  4214. * @stable ICU 2.0
  4215. */
  4216. //--public static com.ibm.icu.math.BigDecimal valueOf(double dub){
  4217. //-- // Reminder: a zero double returns '0.0', so we cannot fastpath to
  4218. //-- // use the constant ZERO. This might be important enough to justify
  4219. //-- // a factory approach, a cache, or a few private constants, later.
  4220. //-- return new com.ibm.icu.math.BigDecimal((new java.lang.Double(dub)).toString());
  4221. //-- }
  4222. /**
  4223. * Translates a <code>long</code> to a <code>BigDecimal</code>.
  4224. * That is, returns a plain <code>BigDecimal</code> whose value is
  4225. * equal to the given <code>long</code>.
  4226. *
  4227. * @param lint The <code>long</code> to be translated.
  4228. * @return The <code>BigDecimal</code> equal in value to
  4229. * <code>lint</code>.
  4230. * @stable ICU 2.0
  4231. */
  4232. //--public static com.ibm.icu.math.BigDecimal valueOf(long lint){
  4233. //-- return valueOf(lint,0);
  4234. //-- }
  4235. /**
  4236. * Translates a <code>long</code> to a <code>BigDecimal</code> with a
  4237. * given scale.
  4238. * That is, returns a plain <code>BigDecimal</code> whose unscaled
  4239. * value is equal to the given <code>long</code>, adjusted by the
  4240. * second parameter, <code>scale</code>.
  4241. * <p>
  4242. * The result is given by:
  4243. * <p><code>
  4244. * (new BigDecimal(lint)).divide(TEN.pow(new BigDecimal(scale)))
  4245. * </code>
  4246. * <p>
  4247. * A <code>NumberFormatException</code> is thrown if <code>scale</code>
  4248. * is negative.
  4249. *
  4250. * @param lint The <code>long</code> to be translated.
  4251. * @param scale The <code>int</code> scale to be applied.
  4252. * @return The <code>BigDecimal</code> equal in value to
  4253. * <code>lint</code>.
  4254. * @throws NumberFormatException if the scale is negative.
  4255. * @stable ICU 2.0
  4256. */
  4257. //--public static com.ibm.icu.math.BigDecimal valueOf(long lint,int scale){
  4258. //-- com.ibm.icu.math.BigDecimal res=null;
  4259. //-- {/*select*/
  4260. //-- if (lint==0)
  4261. //-- res=ZERO;
  4262. //-- else if (lint==1)
  4263. //-- res=ONE;
  4264. //-- else if (lint==10)
  4265. //-- res=TEN;
  4266. //-- else{
  4267. //-- res=new com.ibm.icu.math.BigDecimal(lint);
  4268. //-- }
  4269. //-- }
  4270. //-- if (scale==0)
  4271. //-- return res;
  4272. //-- if (scale<0)
  4273. //-- throw new java.lang.NumberFormatException("Negative scale:"+" "+scale);
  4274. //-- res=clone(res); // safe copy [do not mutate]
  4275. //-- res.exp=(int)-scale; // exponent is -scale
  4276. //-- return res;
  4277. //-- }
  4278. /* ---------------------------------------------------------------- */
  4279. /* Private methods */
  4280. /* ---------------------------------------------------------------- */
  4281. /* <sgml> Return char array value of a BigDecimal (conversion from
  4282. BigDecimal to laid-out canonical char array).
  4283. <p>The mantissa will either already have been rounded (following an
  4284. operation) or will be of length appropriate (in the case of
  4285. construction from an int, for example).
  4286. <p>We must not alter the mantissa, here.
  4287. <p>'form' describes whether we are to use exponential notation (and
  4288. if so, which), or if we are to lay out as a plain/pure numeric.
  4289. </sgml> */
  4290. //--private char[] layout(){
  4291. function layout() {
  4292. //--char cmant[];
  4293. var cmant;
  4294. //--int i=0;
  4295. var i=0;
  4296. //--java.lang.StringBuffer sb=null;
  4297. var sb=null;
  4298. //--int euse=0;
  4299. var euse=0;
  4300. //--int sig=0;
  4301. var sig=0;
  4302. //--char csign=0;
  4303. var csign=0;
  4304. //--char rec[]=null;
  4305. var rec=null;
  4306. //--int needsign;
  4307. var needsign;
  4308. //--int mag;
  4309. var mag;
  4310. //--int len=0;
  4311. var len=0;
  4312. cmant=new Array(this.mant.length); // copy byte[] to a char[]
  4313. {var $18=this.mant.length;i=0;i:for(;$18>0;$18--,i++){
  4314. cmant[i]=this.mant[i]+'';
  4315. }
  4316. }/*i*/
  4317. if (this.form!=MathContext.prototype.PLAIN)
  4318. {/* exponential notation needed */
  4319. //--sb=new java.lang.StringBuffer(cmant.length+15); // -x.xxxE+999999999
  4320. sb="";
  4321. if (this.ind==this.isneg)
  4322. sb += '-';
  4323. euse=(this.exp+cmant.length)-1; // exponent to use
  4324. /* setup sig=significant digits and copy to result */
  4325. if (this.form==MathContext.prototype.SCIENTIFIC)
  4326. { // [default]
  4327. sb += cmant[0]; // significant character
  4328. if (cmant.length>1) // have decimal part
  4329. //--sb.append('.').append(cmant,1,cmant.length-1);
  4330. sb += '.';
  4331. sb += cmant.slice(1).join("");
  4332. }
  4333. else
  4334. {engineering:do{
  4335. sig=euse%3; // common
  4336. if (sig<0)
  4337. sig=3+sig; // negative exponent
  4338. euse=euse-sig;
  4339. sig++;
  4340. if (sig>=cmant.length)
  4341. { // zero padding may be needed
  4342. //--sb.append(cmant,0,cmant.length);
  4343. sb += cmant.join("");
  4344. {var $19=sig-cmant.length;for(;$19>0;$19--){
  4345. sb += '0';
  4346. }
  4347. }
  4348. }
  4349. else
  4350. { // decimal point needed
  4351. //--sb.append(cmant,0,sig).append('.').append(cmant,sig,cmant.length-sig);
  4352. sb += cmant.slice(0,sig).join("");
  4353. sb += '.';
  4354. sb += cmant.slice(sig).join("");
  4355. }
  4356. }while(false);}/*engineering*/
  4357. if (euse!=0)
  4358. {
  4359. if (euse<0)
  4360. {
  4361. csign='-';
  4362. euse=-euse;
  4363. }
  4364. else
  4365. csign='+';
  4366. //--sb.append('E').append(csign).append(euse);
  4367. sb += 'E';
  4368. sb += csign;
  4369. sb += euse;
  4370. }
  4371. //--rec=new Array(sb.length);
  4372. //--Utility.getChars(sb, 0,sb.length(),rec,0);
  4373. //--return rec;
  4374. return sb.split("");
  4375. }
  4376. /* Here for non-exponential (plain) notation */
  4377. if (this.exp==0)
  4378. {/* easy */
  4379. if (this.ind>=0)
  4380. return cmant; // non-negative integer
  4381. rec=new Array(cmant.length+1);
  4382. rec[0]='-';
  4383. //--java.lang.System.arraycopy((java.lang.Object)cmant,0,(java.lang.Object)rec,1,cmant.length);
  4384. this.arraycopy(cmant,0,rec,1,cmant.length);
  4385. return rec;
  4386. }
  4387. /* Need a '.' and/or some zeros */
  4388. needsign=((this.ind==this.isneg)?1:0); // space for sign? 0 or 1
  4389. /* MAG is the position of the point in the mantissa (index of the
  4390. character it follows) */
  4391. mag=this.exp+cmant.length;
  4392. if (mag<1)
  4393. {/* 0.00xxxx form */
  4394. len=(needsign+2)-this.exp; // needsign+2+(-mag)+cmant.length
  4395. rec=new Array(len);
  4396. if (needsign!=0)
  4397. rec[0]='-';
  4398. rec[needsign]='0';
  4399. rec[needsign+1]='.';
  4400. {var $20=-mag;i=needsign+2;i:for(;$20>0;$20--,i++){ // maybe none
  4401. rec[i]='0';
  4402. }
  4403. }/*i*/
  4404. //--java.lang.System.arraycopy((java.lang.Object)cmant,0,(java.lang.Object)rec,(needsign+2)-mag,cmant.length);
  4405. this.arraycopy(cmant,0,rec,(needsign+2)-mag,cmant.length);
  4406. return rec;
  4407. }
  4408. if (mag>cmant.length)
  4409. {/* xxxx0000 form */
  4410. len=needsign+mag;
  4411. rec=new Array(len);
  4412. if (needsign!=0)
  4413. rec[0]='-';
  4414. //--java.lang.System.arraycopy((java.lang.Object)cmant,0,(java.lang.Object)rec,needsign,cmant.length);
  4415. this.arraycopy(cmant,0,rec,needsign,cmant.length);
  4416. {var $21=mag-cmant.length;i=needsign+cmant.length;i:for(;$21>0;$21--,i++){ // never 0
  4417. rec[i]='0';
  4418. }
  4419. }/*i*/
  4420. return rec;
  4421. }
  4422. /* decimal point is in the middle of the mantissa */
  4423. len=(needsign+1)+cmant.length;
  4424. rec=new Array(len);
  4425. if (needsign!=0)
  4426. rec[0]='-';
  4427. //--java.lang.System.arraycopy((java.lang.Object)cmant,0,(java.lang.Object)rec,needsign,mag);
  4428. this.arraycopy(cmant,0,rec,needsign,mag);
  4429. rec[needsign+mag]='.';
  4430. //--java.lang.System.arraycopy((java.lang.Object)cmant,mag,(java.lang.Object)rec,(needsign+mag)+1,cmant.length-mag);
  4431. this.arraycopy(cmant,mag,rec,(needsign+mag)+1,cmant.length-mag);
  4432. return rec;
  4433. }
  4434. /* <sgml> Checks a BigDecimal argument to ensure it's a true integer
  4435. in a given range.
  4436. <p>If OK, returns it as an int. </sgml> */
  4437. // [currently only used by pow]
  4438. //--private int intcheck(int min,int max){
  4439. function intcheck(min, max) {
  4440. //--int i;
  4441. var i;
  4442. i=this.intValueExact(); // [checks for non-0 decimal part]
  4443. // Use same message as though intValueExact failed due to size
  4444. if ((i<min)||(i>max))
  4445. throw "intcheck(): Conversion overflow: "+i;
  4446. return i;
  4447. }
  4448. /* <sgml> Carry out division operations. </sgml> */
  4449. /*
  4450. Arg1 is operation code: D=divide, I=integer divide, R=remainder
  4451. Arg2 is the rhs.
  4452. Arg3 is the context.
  4453. Arg4 is explicit scale iff code='D' or 'I' (-1 if none).
  4454. Underlying algorithm (complications for Remainder function and
  4455. scaled division are omitted for clarity):
  4456. Test for x/0 and then 0/x
  4457. Exp =Exp1 - Exp2
  4458. Exp =Exp +len(var1) -len(var2)
  4459. Sign=Sign1 * Sign2
  4460. Pad accumulator (Var1) to double-length with 0's (pad1)
  4461. Pad Var2 to same length as Var1
  4462. B2B=1st two digits of var2, +1 to allow for roundup
  4463. have=0
  4464. Do until (have=digits+1 OR residue=0)
  4465. if exp<0 then if integer divide/residue then leave
  4466. this_digit=0
  4467. Do forever
  4468. compare numbers
  4469. if <0 then leave inner_loop
  4470. if =0 then (- quick exit without subtract -) do
  4471. this_digit=this_digit+1; output this_digit
  4472. leave outer_loop; end
  4473. Compare lengths of numbers (mantissae):
  4474. If same then CA=first_digit_of_Var1
  4475. else CA=first_two_digits_of_Var1
  4476. mult=ca*10/b2b -- Good and safe guess at divisor
  4477. if mult=0 then mult=1
  4478. this_digit=this_digit+mult
  4479. subtract
  4480. end inner_loop
  4481. if have\=0 | this_digit\=0 then do
  4482. output this_digit
  4483. have=have+1; end
  4484. var2=var2/10
  4485. exp=exp-1
  4486. end outer_loop
  4487. exp=exp+1 -- set the proper exponent
  4488. if have=0 then generate answer=0
  4489. Return to FINISHED
  4490. Result defined by MATHV1
  4491. For extended commentary, see DMSRCN.
  4492. */
  4493. //--private com.ibm.icu.math.BigDecimal dodivide(char code,com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set,int scale){
  4494. function dodivide(code, rhs, set, scale) {
  4495. //--com.ibm.icu.math.BigDecimal lhs;
  4496. var lhs;
  4497. //--int reqdig;
  4498. var reqdig;
  4499. //--int newexp;
  4500. var newexp;
  4501. //--com.ibm.icu.math.BigDecimal res;
  4502. var res;
  4503. //--int newlen;
  4504. var newlen;
  4505. //--byte var1[];
  4506. var var1;
  4507. //--int var1len;
  4508. var var1len;
  4509. //--byte var2[];
  4510. var var2;
  4511. //--int var2len;
  4512. var var2len;
  4513. //--int b2b;
  4514. var b2b;
  4515. //--int have;
  4516. var have;
  4517. //--int thisdigit=0;
  4518. var thisdigit=0;
  4519. //--int i=0;
  4520. var i=0;
  4521. //--byte v2=0;
  4522. var v2=0;
  4523. //--int ba=0;
  4524. var ba=0;
  4525. //--int mult=0;
  4526. var mult=0;
  4527. //--int start=0;
  4528. var start=0;
  4529. //--int padding=0;
  4530. var padding=0;
  4531. //--int d=0;
  4532. var d=0;
  4533. //--byte newvar1[]=null;
  4534. var newvar1=null;
  4535. //--byte lasthave=0;
  4536. var lasthave=0;
  4537. //--int actdig=0;
  4538. var actdig=0;
  4539. //--byte newmant[]=null;
  4540. var newmant=null;
  4541. if (set.lostDigits)
  4542. this.checkdigits(rhs,set.digits);
  4543. lhs=this; // name for clarity
  4544. // [note we must have checked lostDigits before the following checks]
  4545. if (rhs.ind==0)
  4546. throw "dodivide(): Divide by 0"; // includes 0/0
  4547. if (lhs.ind==0)
  4548. { // 0/x => 0 [possibly with .0s]
  4549. if (set.form!=MathContext.prototype.PLAIN)
  4550. return this.ZERO;
  4551. if (scale==(-1))
  4552. return lhs;
  4553. return lhs.setScale(scale);
  4554. }
  4555. /* Prepare numbers according to BigDecimal rules */
  4556. reqdig=set.digits; // local copy (heavily used)
  4557. if (reqdig>0)
  4558. {
  4559. if (lhs.mant.length>reqdig)
  4560. lhs=this.clone(lhs).round(set);
  4561. if (rhs.mant.length>reqdig)
  4562. rhs=this.clone(rhs).round(set);
  4563. }
  4564. else
  4565. {/* scaled divide */
  4566. if (scale==(-1))
  4567. scale=lhs.scale();
  4568. // set reqdig to be at least large enough for the computation
  4569. reqdig=lhs.mant.length; // base length
  4570. // next line handles both positive lhs.exp and also scale mismatch
  4571. if (scale!=(-lhs.exp))
  4572. reqdig=(reqdig+scale)+lhs.exp;
  4573. reqdig=(reqdig-((rhs.mant.length-1)))-rhs.exp; // reduce by RHS effect
  4574. if (reqdig<lhs.mant.length)
  4575. reqdig=lhs.mant.length; // clamp
  4576. if (reqdig<rhs.mant.length)
  4577. reqdig=rhs.mant.length; // ..
  4578. }
  4579. /* precalculate exponent */
  4580. newexp=((lhs.exp-rhs.exp)+lhs.mant.length)-rhs.mant.length;
  4581. /* If new exponent -ve, then some quick exits are possible */
  4582. if (newexp<0)
  4583. if (code!='D')
  4584. {
  4585. if (code=='I')
  4586. return this.ZERO; // easy - no integer part
  4587. /* Must be 'R'; remainder is [finished clone of] input value */
  4588. return this.clone(lhs).finish(set,false);
  4589. }
  4590. /* We need slow division */
  4591. res=new BigDecimal(); // where we'll build result
  4592. res.ind=(lhs.ind*rhs.ind); // final sign (for D/I)
  4593. res.exp=newexp; // initial exponent (for D/I)
  4594. res.mant=this.createArrayWithZeros(reqdig+1); // where build the result
  4595. /* Now [virtually pad the mantissae with trailing zeros */
  4596. // Also copy the LHS, which will be our working array
  4597. newlen=(reqdig+reqdig)+1;
  4598. var1=this.extend(lhs.mant,newlen); // always makes longer, so new safe array
  4599. var1len=newlen; // [remaining digits are 0]
  4600. var2=rhs.mant;
  4601. var2len=newlen;
  4602. /* Calculate first two digits of rhs (var2), +1 for later estimations */
  4603. b2b=(var2[0]*10)+1;
  4604. if (var2.length>1)
  4605. b2b=b2b+var2[1];
  4606. /* start the long-division loops */
  4607. have=0;
  4608. {outer:for(;;){
  4609. thisdigit=0;
  4610. /* find the next digit */
  4611. {inner:for(;;){
  4612. if (var1len<var2len)
  4613. break inner; // V1 too low
  4614. if (var1len==var2len)
  4615. { // compare needed
  4616. {compare:do{ // comparison
  4617. {var $22=var1len;i=0;i:for(;$22>0;$22--,i++){
  4618. // var1len is always <= var1.length
  4619. if (i<var2.length)
  4620. v2=var2[i];
  4621. else
  4622. v2=0;
  4623. if (var1[i]<v2)
  4624. break inner; // V1 too low
  4625. if (var1[i]>v2)
  4626. break compare; // OK to subtract
  4627. }
  4628. }/*i*/
  4629. /* reach here if lhs and rhs are identical; subtraction will
  4630. increase digit by one, and the residue will be 0 so we
  4631. are done; leave the loop with residue set to 0 (in case
  4632. code is 'R' or ROUND_UNNECESSARY or a ROUND_HALF_xxxx is
  4633. being checked) */
  4634. thisdigit++;
  4635. res.mant[have]=thisdigit;
  4636. have++;
  4637. var1[0]=0; // residue to 0 [this is all we'll test]
  4638. // var1len=1 -- [optimized out]
  4639. break outer;
  4640. }while(false);}/*compare*/
  4641. /* prepare for subtraction. Estimate BA (lengths the same) */
  4642. ba=var1[0]; // use only first digit
  4643. } // lengths the same
  4644. else
  4645. {/* lhs longer than rhs */
  4646. /* use first two digits for estimate */
  4647. ba=var1[0]*10;
  4648. if (var1len>1)
  4649. ba=ba+var1[1];
  4650. }
  4651. /* subtraction needed; V1>=V2 */
  4652. mult=div((ba*10),b2b);
  4653. if (mult==0)
  4654. mult=1;
  4655. thisdigit=thisdigit+mult;
  4656. // subtract; var1 reusable
  4657. var1=this.byteaddsub(var1,var1len,var2,var2len,-mult,true);
  4658. if (var1[0]!=0)
  4659. continue inner; // maybe another subtract needed
  4660. /* V1 now probably has leading zeros, remove leading 0's and try
  4661. again. (It could be longer than V2) */
  4662. {var $23=var1len-2;start=0;start:for(;start<=$23;start++){
  4663. if (var1[start]!=0)
  4664. break start;
  4665. var1len--;
  4666. }
  4667. }/*start*/
  4668. if (start==0)
  4669. continue inner;
  4670. // shift left
  4671. //--java.lang.System.arraycopy((java.lang.Object)var1,start,(java.lang.Object)var1,0,var1len);
  4672. this.arraycopy(var1,start,var1,0,var1len);
  4673. }
  4674. }/*inner*/
  4675. /* We have the next digit */
  4676. if ((have!=0)||(thisdigit!=0))
  4677. { // put the digit we got
  4678. res.mant[have]=thisdigit;
  4679. have++;
  4680. if (have==(reqdig+1))
  4681. break outer; // we have all we need
  4682. if (var1[0]==0)
  4683. break outer; // residue now 0
  4684. }
  4685. /* can leave now if a scaled divide and exponent is small enough */
  4686. if (scale>=0)
  4687. if ((-res.exp)>scale)
  4688. break outer;
  4689. /* can leave now if not Divide and no integer part left */
  4690. if (code!='D')
  4691. if (res.exp<=0)
  4692. break outer;
  4693. res.exp=res.exp-1; // reduce the exponent
  4694. /* to get here, V1 is less than V2, so divide V2 by 10 and go for
  4695. the next digit */
  4696. var2len--;
  4697. }
  4698. }/*outer*/
  4699. /* here when we have finished dividing, for some reason */
  4700. // have is the number of digits we collected in res.mant
  4701. if (have==0)
  4702. have=1; // res.mant[0] is 0; we always want a digit
  4703. if ((code=='I')||(code=='R'))
  4704. {/* check for integer overflow needed */
  4705. if ((have+res.exp)>reqdig)
  4706. throw "dodivide(): Integer overflow";
  4707. if (code=='R')
  4708. {remainder:do{
  4709. /* We were doing Remainder -- return the residue */
  4710. if (res.mant[0]==0) // no integer part was found
  4711. return this.clone(lhs).finish(set,false); // .. so return lhs, canonical
  4712. if (var1[0]==0)
  4713. return this.ZERO; // simple 0 residue
  4714. res.ind=lhs.ind; // sign is always as LHS
  4715. /* Calculate the exponent by subtracting the number of padding zeros
  4716. we added and adding the original exponent */
  4717. padding=((reqdig+reqdig)+1)-lhs.mant.length;
  4718. res.exp=(res.exp-padding)+lhs.exp;
  4719. /* strip insignificant padding zeros from residue, and create/copy
  4720. the resulting mantissa if need be */
  4721. d=var1len;
  4722. {i=d-1;i:for(;i>=1;i--){if(!((res.exp<lhs.exp)&&(res.exp<rhs.exp)))break;
  4723. if (var1[i]!=0)
  4724. break i;
  4725. d--;
  4726. res.exp=res.exp+1;
  4727. }
  4728. }/*i*/
  4729. if (d<var1.length)
  4730. {/* need to reduce */
  4731. newvar1=new Array(d);
  4732. //--java.lang.System.arraycopy((java.lang.Object)var1,0,(java.lang.Object)newvar1,0,d); // shorten
  4733. this.arraycopy(var1,0,newvar1,0,d);
  4734. var1=newvar1;
  4735. }
  4736. res.mant=var1;
  4737. return res.finish(set,false);
  4738. }while(false);}/*remainder*/
  4739. }
  4740. else
  4741. {/* 'D' -- no overflow check needed */
  4742. // If there was a residue then bump the final digit (iff 0 or 5)
  4743. // so that the residue is visible for ROUND_UP, ROUND_HALF_xxx and
  4744. // ROUND_UNNECESSARY checks (etc.) later.
  4745. // [if we finished early, the residue will be 0]
  4746. if (var1[0]!=0)
  4747. { // residue not 0
  4748. lasthave=res.mant[have-1];
  4749. if (((lasthave%5))==0)
  4750. res.mant[have-1]=(lasthave+1);
  4751. }
  4752. }
  4753. /* Here for Divide or Integer Divide */
  4754. // handle scaled results first ['I' always scale 0, optional for 'D']
  4755. if (scale>=0)
  4756. {scaled:do{
  4757. // say 'scale have res.exp len' scale have res.exp res.mant.length
  4758. if (have!=res.mant.length)
  4759. // already padded with 0's, so just adjust exponent
  4760. res.exp=res.exp-((res.mant.length-have));
  4761. // calculate number of digits we really want [may be 0]
  4762. actdig=res.mant.length-(((-res.exp)-scale));
  4763. res.round(actdig,set.roundingMode); // round to desired length
  4764. // This could have shifted left if round (say) 0.9->1[.0]
  4765. // Repair if so by adding a zero and reducing exponent
  4766. if (res.exp!=(-scale))
  4767. {
  4768. res.mant=this.extend(res.mant,res.mant.length+1);
  4769. res.exp=res.exp-1;
  4770. }
  4771. return res.finish(set,true); // [strip if not PLAIN]
  4772. }while(false);}/*scaled*/
  4773. // reach here only if a non-scaled
  4774. if (have==res.mant.length)
  4775. { // got digits+1 digits
  4776. res.round(set);
  4777. have=reqdig;
  4778. }
  4779. else
  4780. {/* have<=reqdig */
  4781. if (res.mant[0]==0)
  4782. return this.ZERO; // fastpath
  4783. // make the mantissa truly just 'have' long
  4784. // [we could let finish do this, during strip, if we adjusted
  4785. // the exponent; however, truncation avoids the strip loop]
  4786. newmant=new Array(have); // shorten
  4787. //--java.lang.System.arraycopy((java.lang.Object)res.mant,0,(java.lang.Object)newmant,0,have);
  4788. this.arraycopy(res.mant,0,newmant,0,have);
  4789. res.mant=newmant;
  4790. }
  4791. return res.finish(set,true);
  4792. }
  4793. /* <sgml> Report a conversion exception. </sgml> */
  4794. //--private void bad(char s[]){
  4795. function bad(prefix, s) {
  4796. throw prefix + "Not a number: "+s;
  4797. }
  4798. /* <sgml> Report a bad argument to a method. </sgml>
  4799. Arg1 is method name
  4800. Arg2 is argument position
  4801. Arg3 is what was found */
  4802. //--private void badarg(java.lang.String name,int pos,java.lang.String value){
  4803. function badarg(name, pos, value) {
  4804. throw "Bad argument "+pos+" to "+name+": "+value;
  4805. }
  4806. /* <sgml> Extend byte array to given length, padding with 0s. If no
  4807. extension is required then return the same array. </sgml>
  4808. Arg1 is the source byte array
  4809. Arg2 is the new length (longer)
  4810. */
  4811. //--private static final byte[] extend(byte inarr[],int newlen){
  4812. function extend(inarr, newlen) {
  4813. //--byte newarr[];
  4814. var newarr;
  4815. if (inarr.length==newlen)
  4816. return inarr;
  4817. newarr=createArrayWithZeros(newlen);
  4818. //--java.lang.System.arraycopy((java.lang.Object)inarr,0,(java.lang.Object)newarr,0,inarr.length);
  4819. this.arraycopy(inarr,0,newarr,0,inarr.length);
  4820. // 0 padding is carried out by the JVM on allocation initialization
  4821. return newarr;
  4822. }
  4823. /* <sgml> Add or subtract two >=0 integers in byte arrays
  4824. <p>This routine performs the calculation:
  4825. <pre>
  4826. C=A+(B*M)
  4827. </pre>
  4828. Where M is in the range -9 through +9
  4829. <p>
  4830. If M<0 then A>=B must be true, so the result is always
  4831. non-negative.
  4832. Leading zeros are not removed after a subtraction. The result is
  4833. either the same length as the longer of A and B, or 1 longer than
  4834. that (if a carry occurred).
  4835. A is not altered unless Arg6 is 1.
  4836. B is never altered.
  4837. Arg1 is A
  4838. Arg2 is A length to use (if longer than A, pad with 0's)
  4839. Arg3 is B
  4840. Arg4 is B length to use (if longer than B, pad with 0's)
  4841. Arg5 is M, the multiplier
  4842. Arg6 is 1 if A can be used to build the result (if it fits)
  4843. This routine is severely performance-critical; *any* change here
  4844. must be measured (timed) to assure no performance degradation.
  4845. */
  4846. // 1996.02.20 -- enhanced version of DMSRCN algorithm (1981)
  4847. // 1997.10.05 -- changed to byte arrays (from char arrays)
  4848. // 1998.07.01 -- changed to allow destructive reuse of LHS
  4849. // 1998.07.01 -- changed to allow virtual lengths for the arrays
  4850. // 1998.12.29 -- use lookaside for digit/carry calculation
  4851. // 1999.08.07 -- avoid multiply when mult=1, and make db an int
  4852. // 1999.12.22 -- special case m=-1, also drop 0 special case
  4853. //--private static final byte[] byteaddsub(byte a[],int avlen,byte b[],int bvlen,int m,boolean reuse){
  4854. function byteaddsub(a, avlen, b, bvlen, m, reuse) {
  4855. //--int alength;
  4856. var alength;
  4857. //--int blength;
  4858. var blength;
  4859. //--int ap;
  4860. var ap;
  4861. //--int bp;
  4862. var bp;
  4863. //--int maxarr;
  4864. var maxarr;
  4865. //--byte reb[];
  4866. var reb;
  4867. //--boolean quickm;
  4868. var quickm;
  4869. //--int digit;
  4870. var digit;
  4871. //--int op=0;
  4872. var op=0;
  4873. //--int dp90=0;
  4874. var dp90=0;
  4875. //--byte newarr[];
  4876. var newarr;
  4877. //--int i=0;
  4878. var i=0;
  4879. // We'll usually be right if we assume no carry
  4880. alength=a.length; // physical lengths
  4881. blength=b.length; // ..
  4882. ap=avlen-1; // -> final (rightmost) digit
  4883. bp=bvlen-1; // ..
  4884. maxarr=bp;
  4885. if (maxarr<ap)
  4886. maxarr=ap;
  4887. reb=null; // result byte array
  4888. if (reuse)
  4889. if ((maxarr+1)==alength)
  4890. reb=a; // OK to reuse A
  4891. if (reb==null){
  4892. reb=this.createArrayWithZeros(maxarr+1); // need new array
  4893. }
  4894. quickm=false; // 1 if no multiply needed
  4895. if (m==1)
  4896. quickm=true; // most common
  4897. else
  4898. if (m==(-1))
  4899. quickm=true; // also common
  4900. digit=0; // digit, with carry or borrow
  4901. {op=maxarr;op:for(;op>=0;op--){
  4902. if (ap>=0)
  4903. {
  4904. if (ap<alength)
  4905. digit=digit+a[ap]; // within A
  4906. ap--;
  4907. }
  4908. if (bp>=0)
  4909. {
  4910. if (bp<blength)
  4911. { // within B
  4912. if (quickm)
  4913. {
  4914. if (m>0)
  4915. digit=digit+b[bp]; // most common
  4916. else
  4917. digit=digit-b[bp]; // also common
  4918. }
  4919. else
  4920. digit=digit+(b[bp]*m);
  4921. }
  4922. bp--;
  4923. }
  4924. /* result so far (digit) could be -90 through 99 */
  4925. if (digit<10)
  4926. if (digit>=0)
  4927. {quick:do{ // 0-9
  4928. reb[op]=digit;
  4929. digit=0; // no carry
  4930. continue op;
  4931. }while(false);}/*quick*/
  4932. dp90=digit+90;
  4933. reb[op]=this.bytedig[dp90]; // this digit
  4934. digit=this.bytecar[dp90]; // carry or borrow
  4935. }
  4936. }/*op*/
  4937. if (digit==0)
  4938. return reb; // no carry
  4939. // following line will become an Assert, later
  4940. // if digit<0 then signal ArithmeticException("internal.error ["digit"]")
  4941. /* We have carry -- need to make space for the extra digit */
  4942. newarr=null;
  4943. if (reuse)
  4944. if ((maxarr+2)==a.length)
  4945. newarr=a; // OK to reuse A
  4946. if (newarr==null)
  4947. newarr=new Array(maxarr+2);
  4948. newarr[0]=digit; // the carried digit ..
  4949. // .. and all the rest [use local loop for short numbers]
  4950. //--if (maxarr<10)
  4951. {var $24=maxarr+1;i=0;i:for(;$24>0;$24--,i++){
  4952. newarr[i+1]=reb[i];
  4953. }
  4954. }/*i*/
  4955. //--else
  4956. //--java.lang.System.arraycopy((java.lang.Object)reb,0,(java.lang.Object)newarr,1,maxarr+1);
  4957. return newarr;
  4958. }
  4959. /* <sgml> Initializer for digit array properties (lookaside). </sgml>
  4960. Returns the digit array, and initializes the carry array. */
  4961. //--private static final byte[] diginit(){
  4962. function diginit() {
  4963. //--byte work[];
  4964. var work;
  4965. //--int op=0;
  4966. var op=0;
  4967. //--int digit=0;
  4968. var digit=0;
  4969. work=new Array((90+99)+1);
  4970. {op=0;op:for(;op<=(90+99);op++){
  4971. digit=op-90;
  4972. if (digit>=0)
  4973. {
  4974. work[op]=(digit%10);
  4975. BigDecimal.prototype.bytecar[op]=(div(digit,10)); // calculate carry
  4976. continue op;
  4977. }
  4978. // borrowing...
  4979. digit=digit+100; // yes, this is right [consider -50]
  4980. work[op]=(digit%10);
  4981. BigDecimal.prototype.bytecar[op]=((div(digit,10))-10); // calculate borrow [NB: - after %]
  4982. }
  4983. }/*op*/
  4984. return work;
  4985. }
  4986. /* <sgml> Create a copy of BigDecimal object for local use.
  4987. <p>This does NOT make a copy of the mantissa array.
  4988. </sgml>
  4989. Arg1 is the BigDecimal to clone (non-null)
  4990. */
  4991. //--private static final com.ibm.icu.math.BigDecimal clone(com.ibm.icu.math.BigDecimal dec){
  4992. function clone(dec) {
  4993. //--com.ibm.icu.math.BigDecimal copy;
  4994. var copy;
  4995. copy=new BigDecimal();
  4996. copy.ind=dec.ind;
  4997. copy.exp=dec.exp;
  4998. copy.form=dec.form;
  4999. copy.mant=dec.mant;
  5000. return copy;
  5001. }
  5002. /* <sgml> Check one or two numbers for lost digits. </sgml>
  5003. Arg1 is RHS (or null, if none)
  5004. Arg2 is current DIGITS setting
  5005. returns quietly or throws an exception */
  5006. //--private void checkdigits(com.ibm.icu.math.BigDecimal rhs,int dig){
  5007. function checkdigits(rhs, dig) {
  5008. if (dig==0)
  5009. return; // don't check if digits=0
  5010. // first check lhs...
  5011. if (this.mant.length>dig)
  5012. if ((!(this.allzero(this.mant,dig))))
  5013. throw "Too many digits: "+this.toString();
  5014. if (rhs==null)
  5015. return; // monadic
  5016. if (rhs.mant.length>dig)
  5017. if ((!(this.allzero(rhs.mant,dig))))
  5018. throw "Too many digits: "+rhs.toString();
  5019. return;
  5020. }
  5021. /* <sgml> Round to specified digits, if necessary. </sgml>
  5022. Arg1 is requested MathContext [with length and rounding mode]
  5023. returns this, for convenience */
  5024. //--private com.ibm.icu.math.BigDecimal round(com.ibm.icu.math.MathContext set){
  5025. //-- return round(set.digits,set.roundingMode);
  5026. //-- }
  5027. /* <sgml> Round to specified digits, if necessary.
  5028. Arg1 is requested length (digits to round to)
  5029. [may be <=0 when called from format, dodivide, etc.]
  5030. Arg2 is rounding mode
  5031. returns this, for convenience
  5032. ind and exp are adjusted, but not cleared for a mantissa of zero
  5033. The length of the mantissa returned will be Arg1, except when Arg1
  5034. is 0, in which case the returned mantissa length will be 1.
  5035. </sgml>
  5036. */
  5037. //private com.ibm.icu.math.BigDecimal round(int len,int mode){
  5038. function round() {
  5039. var len;
  5040. var mode;
  5041. if (round.arguments.length == 2)
  5042. {
  5043. len = round.arguments[0];
  5044. mode = round.arguments[1];
  5045. }
  5046. else if (round.arguments.length == 1)
  5047. {
  5048. var set = round.arguments[0];
  5049. len = set.digits;
  5050. mode = set.roundingMode;
  5051. }
  5052. else
  5053. {
  5054. throw "round(): " + round.arguments.length + " arguments given; expected 1 or 2";
  5055. }
  5056. //int adjust;
  5057. var adjust;
  5058. //int sign;
  5059. var sign;
  5060. //byte oldmant[];
  5061. var oldmant;
  5062. //boolean reuse=false;
  5063. var reuse=false;
  5064. //--byte first=0;
  5065. var first=0;
  5066. //--int increment;
  5067. var increment;
  5068. //--byte newmant[]=null;
  5069. var newmant=null;
  5070. adjust=this.mant.length-len;
  5071. if (adjust<=0)
  5072. return this; // nowt to do
  5073. this.exp=this.exp+adjust; // exponent of result
  5074. sign=this.ind; // save [assumes -1, 0, 1]
  5075. oldmant=this.mant; // save
  5076. if (len>0)
  5077. {
  5078. // remove the unwanted digits
  5079. this.mant=new Array(len);
  5080. //--java.lang.System.arraycopy((java.lang.Object)oldmant,0,(java.lang.Object)mant,0,len);
  5081. this.arraycopy(oldmant,0,this.mant,0,len);
  5082. reuse=true; // can reuse mantissa
  5083. first=oldmant[len]; // first of discarded digits
  5084. }
  5085. else
  5086. {/* len<=0 */
  5087. this.mant=this.ZERO.mant;
  5088. this.ind=this.iszero;
  5089. reuse=false; // cannot reuse mantissa
  5090. if (len==0)
  5091. first=oldmant[0];
  5092. else
  5093. first=0; // [virtual digit]
  5094. }
  5095. // decide rounding adjustment depending on mode, sign, and discarded digits
  5096. increment=0; // bumper
  5097. {modes:do{/*select*/
  5098. if (mode==this.ROUND_HALF_UP)
  5099. { // default first [most common]
  5100. if (first>=5)
  5101. increment=sign;
  5102. }
  5103. else if (mode==this.ROUND_UNNECESSARY)
  5104. { // default for setScale()
  5105. // discarding any non-zero digits is an error
  5106. if ((!(this.allzero(oldmant,len))))
  5107. throw "round(): Rounding necessary";
  5108. }
  5109. else if (mode==this.ROUND_HALF_DOWN)
  5110. { // 0.5000 goes down
  5111. if (first>5)
  5112. increment=sign;
  5113. else
  5114. if (first==5)
  5115. if ((!(this.allzero(oldmant,len+1))))
  5116. increment=sign;
  5117. }
  5118. else if (mode==this.ROUND_HALF_EVEN)
  5119. { // 0.5000 goes down if left digit even
  5120. if (first>5)
  5121. increment=sign;
  5122. else
  5123. if (first==5)
  5124. {
  5125. if ((!(this.allzero(oldmant,len+1))))
  5126. increment=sign;
  5127. else /* 0.5000 */
  5128. if ((((this.mant[this.mant.length-1])%2))==1)
  5129. increment=sign;
  5130. }
  5131. }
  5132. else if (mode==this.ROUND_DOWN)
  5133. {} // never increment
  5134. else if (mode==this.ROUND_UP)
  5135. { // increment if discarded non-zero
  5136. if ((!(this.allzero(oldmant,len))))
  5137. increment=sign;
  5138. }
  5139. else if (mode==this.ROUND_CEILING)
  5140. { // more positive
  5141. if (sign>0)
  5142. if ((!(this.allzero(oldmant,len))))
  5143. increment=sign;
  5144. }
  5145. else if (mode==this.ROUND_FLOOR)
  5146. { // more negative
  5147. if (sign<0)
  5148. if ((!(this.allzero(oldmant,len))))
  5149. increment=sign;
  5150. }
  5151. else{
  5152. throw "round(): Bad round value: "+mode;
  5153. }
  5154. }while(false);}/*modes*/
  5155. if (increment!=0)
  5156. {bump:do{
  5157. if (this.ind==this.iszero)
  5158. {
  5159. // we must not subtract from 0, but result is trivial anyway
  5160. this.mant=this.ONE.mant;
  5161. this.ind=increment;
  5162. }
  5163. else
  5164. {
  5165. // mantissa is non-0; we can safely add or subtract 1
  5166. if (this.ind==this.isneg)
  5167. increment=-increment;
  5168. newmant=this.byteaddsub(this.mant,this.mant.length,this.ONE.mant,1,increment,reuse);
  5169. if (newmant.length>this.mant.length)
  5170. { // had a carry
  5171. // drop rightmost digit and raise exponent
  5172. this.exp++;
  5173. // mant is already the correct length
  5174. //java.lang.System.arraycopy((java.lang.Object)newmant,0,(java.lang.Object)mant,0,mant.length);
  5175. this.arraycopy(newmant,0,this.mant,0,this.mant.length);
  5176. }
  5177. else
  5178. this.mant=newmant;
  5179. }
  5180. }while(false);}/*bump*/
  5181. // rounding can increase exponent significantly
  5182. if (this.exp>this.MaxExp)
  5183. throw "round(): Exponent Overflow: "+this.exp;
  5184. return this;
  5185. }
  5186. /* <sgml> Test if rightmost digits are all 0.
  5187. Arg1 is a mantissa array to test
  5188. Arg2 is the offset of first digit to check
  5189. [may be negative; if so, digits to left are 0's]
  5190. returns 1 if all the digits starting at Arg2 are 0
  5191. Arg2 may be beyond array bounds, in which case 1 is returned
  5192. </sgml> */
  5193. //--private static final boolean allzero(byte array[],int start){
  5194. function allzero(array, start) {
  5195. //--int i=0;
  5196. var i=0;
  5197. if (start<0)
  5198. start=0;
  5199. {var $25=array.length-1;i=start;i:for(;i<=$25;i++){
  5200. if (array[i]!=0)
  5201. return false;
  5202. }
  5203. }/*i*/
  5204. return true;
  5205. }
  5206. /* <sgml> Carry out final checks and canonicalization
  5207. <p>
  5208. This finishes off the current number by:
  5209. 1. Rounding if necessary (NB: length includes leading zeros)
  5210. 2. Stripping trailing zeros (if requested and \PLAIN)
  5211. 3. Stripping leading zeros (always)
  5212. 4. Selecting exponential notation (if required)
  5213. 5. Converting a zero result to just '0' (if \PLAIN)
  5214. In practice, these operations overlap and share code.
  5215. It always sets form.
  5216. </sgml>
  5217. Arg1 is requested MathContext (length to round to, trigger, and FORM)
  5218. Arg2 is 1 if trailing insignificant zeros should be removed after
  5219. round (for division, etc.), provided that set.form isn't PLAIN.
  5220. returns this, for convenience
  5221. */
  5222. //--private com.ibm.icu.math.BigDecimal finish(com.ibm.icu.math.MathContext set,boolean strip){
  5223. function finish(set, strip) {
  5224. //--int d=0;
  5225. var d=0;
  5226. //--int i=0;
  5227. var i=0;
  5228. //--byte newmant[]=null;
  5229. var newmant=null;
  5230. //--int mag=0;
  5231. var mag=0;
  5232. //--int sig=0;
  5233. var sig=0;
  5234. /* Round if mantissa too long and digits requested */
  5235. if (set.digits!=0)
  5236. if (this.mant.length>set.digits)
  5237. this.round(set);
  5238. /* If strip requested (and standard formatting), remove
  5239. insignificant trailing zeros. */
  5240. if (strip)
  5241. if (set.form!=MathContext.prototype.PLAIN)
  5242. {
  5243. d=this.mant.length;
  5244. /* see if we need to drop any trailing zeros */
  5245. {i=d-1;i:for(;i>=1;i--){
  5246. if (this.mant[i]!=0)
  5247. break i;
  5248. d--;
  5249. this.exp++;
  5250. }
  5251. }/*i*/
  5252. if (d<this.mant.length)
  5253. {/* need to reduce */
  5254. newmant=new Array(d);
  5255. //--java.lang.System.arraycopy((java.lang.Object)this.mant,0,(java.lang.Object)newmant,0,d);
  5256. this.arraycopy(this.mant,0,newmant,0,d);
  5257. this.mant=newmant;
  5258. }
  5259. }
  5260. this.form=MathContext.prototype.PLAIN; // preset
  5261. /* Now check for leading- and all- zeros in mantissa */
  5262. {var $26=this.mant.length;i=0;i:for(;$26>0;$26--,i++){
  5263. if (this.mant[i]!=0)
  5264. {
  5265. // non-0 result; ind will be correct
  5266. // remove leading zeros [e.g., after subtract]
  5267. if (i>0)
  5268. {delead:do{
  5269. newmant=new Array(this.mant.length-i);
  5270. //--java.lang.System.arraycopy((java.lang.Object)this.mant,i,(java.lang.Object)newmant,0,this.mant.length-i);
  5271. this.arraycopy(this.mant,i,newmant,0,this.mant.length-i);
  5272. this.mant=newmant;
  5273. }while(false);}/*delead*/
  5274. // now determine form if not PLAIN
  5275. mag=this.exp+this.mant.length;
  5276. if (mag>0)
  5277. { // most common path
  5278. if (mag>set.digits)
  5279. if (set.digits!=0)
  5280. this.form=set.form;
  5281. if ((mag-1)<=this.MaxExp)
  5282. return this; // no overflow; quick return
  5283. }
  5284. else
  5285. if (mag<(-5))
  5286. this.form=set.form;
  5287. /* check for overflow */
  5288. mag--;
  5289. if ((mag<this.MinExp)||(mag>this.MaxExp))
  5290. {overflow:do{
  5291. // possible reprieve if form is engineering
  5292. if (this.form==MathContext.prototype.ENGINEERING)
  5293. {
  5294. sig=mag%3; // leftover
  5295. if (sig<0)
  5296. sig=3+sig; // negative exponent
  5297. mag=mag-sig; // exponent to use
  5298. // 1999.06.29: second test here must be MaxExp
  5299. if (mag>=this.MinExp)
  5300. if (mag<=this.MaxExp)
  5301. break overflow;
  5302. }
  5303. throw "finish(): Exponent Overflow: "+mag;
  5304. }while(false);}/*overflow*/
  5305. return this;
  5306. }
  5307. }
  5308. }/*i*/
  5309. // Drop through to here only if mantissa is all zeros
  5310. this.ind=this.iszero;
  5311. {/*select*/
  5312. if (set.form!=MathContext.prototype.PLAIN)
  5313. this.exp=0; // standard result; go to '0'
  5314. else if (this.exp>0)
  5315. this.exp=0; // +ve exponent also goes to '0'
  5316. else{
  5317. // a plain number with -ve exponent; preserve and check exponent
  5318. if (this.exp<this.MinExp)
  5319. throw "finish(): Exponent Overflow: "+this.exp;
  5320. }
  5321. }
  5322. this.mant=this.ZERO.mant; // canonical mantissa
  5323. return this;
  5324. }
  5325. function isGreaterThan(other) {
  5326. return this.compareTo(other) > 0;
  5327. };
  5328. function isLessThan(other) {
  5329. return this.compareTo(other) < 0;
  5330. };
  5331. function isGreaterThanOrEqualTo(other) {
  5332. return this.compareTo(other) >= 0;
  5333. };
  5334. function isLessThanOrEqualTo(other) {
  5335. return this.compareTo(other) <= 0;
  5336. };
  5337. function isPositive() {
  5338. return this.compareTo(BigDecimal.prototype.ZERO) > 0;
  5339. };
  5340. function isNegative() {
  5341. return this.compareTo(BigDecimal.prototype.ZERO) < 0;
  5342. };
  5343. function isZero() {
  5344. return this.compareTo(BigDecimal.prototype.ZERO) === 0;
  5345. };
  5346. return BigDecimal;
  5347. })(MathContext); // BigDecimal depends on MathContext
  5348. if (typeof define === "function" && define.amd != null) {
  5349. // AMD-loader compatible resource declaration
  5350. // require('bigdecimal') will return JS Object:
  5351. // {'BigDecimal':BigDecimalPointer, 'MathContext':MathContextPointer}
  5352. define({'BigDecimal':BigDecimal, 'MathContext':MathContext});
  5353. } else if (typeof this === "object"){
  5354. // global-polluting outcome.
  5355. this.BigDecimal = BigDecimal;
  5356. this.MathContext = MathContext;
  5357. }
  5358. }).call(this); // in browser 'this' will be 'window' or simulated window object in AMD-loading scenarios.


再来一份min版本的:

  1. /*
  2. Copyright (c) 2012 Daniel Trebbien and other contributors
  3. Portions Copyright (c) 2003 STZ-IDA and PTV AG, Karlsruhe, Germany
  4. Portions Copyright (c) 1995-2001 International Business Machines Corporation and others
  5. All rights reserved.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, provided that the above copyright notice(s) and this permission notice appear in all copies of the Software and that both the above copyright notice(s) and this permission notice appear in supporting documentation.
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  8. Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder.
  9. */
  10. (function(){var y=function(){function h(){this.form=this.digits=0;this.lostDigits=!1;this.roundingMode=0;var v=this.DEFAULT_FORM,r=this.DEFAULT_LOSTDIGITS,f=this.DEFAULT_ROUNDINGMODE;if(4==h.arguments.length)v=h.arguments[1],r=h.arguments[2],f=h.arguments[3];else if(3==h.arguments.length)v=h.arguments[1],r=h.arguments[2];else if(2==h.arguments.length)v=h.arguments[1];else if(1!=h.arguments.length)throw"MathContext(): "+h.arguments.length+" arguments given; expected 1 to 4";var t=h.arguments[0];if(t!=
  11. this.DEFAULT_DIGITS){if(t<this.MIN_DIGITS)throw"MathContext(): Digits too small: "+t;if(t>this.MAX_DIGITS)throw"MathContext(): Digits too large: "+t;}if(v!=this.SCIENTIFIC&&v!=this.ENGINEERING&&v!=this.PLAIN)throw"MathContext() Bad form value: "+v;if(!this.isValidRound(f))throw"MathContext(): Bad roundingMode value: "+f;this.digits=t;this.form=v;this.lostDigits=r;this.roundingMode=f}h.prototype.getDigits=function(){return this.digits};h.prototype.getForm=function(){return this.form};h.prototype.getLostDigits=
  12. function(){return this.lostDigits};h.prototype.getRoundingMode=function(){return this.roundingMode};h.prototype.toString=function(){var h=null,r=0,f=null,h=this.form==this.SCIENTIFIC?"SCIENTIFIC":this.form==this.ENGINEERING?"ENGINEERING":"PLAIN",t=this.ROUNDS.length,r=0;a:for(;0<t;t--,r++)if(this.roundingMode==this.ROUNDS[r]){f=this.ROUNDWORDS[r];break a}return"digits="+this.digits+" form="+h+" lostDigits="+(this.lostDigits?"1":"0")+" roundingMode="+f};h.prototype.isValidRound=function(h){var r=0,
  13. f=this.ROUNDS.length,r=0;for(;0<f;f--,r++)if(h==this.ROUNDS[r])return!0;return!1};h.PLAIN=h.prototype.PLAIN=0;h.SCIENTIFIC=h.prototype.SCIENTIFIC=1;h.ENGINEERING=h.prototype.ENGINEERING=2;h.ROUND_CEILING=h.prototype.ROUND_CEILING=2;h.ROUND_DOWN=h.prototype.ROUND_DOWN=1;h.ROUND_FLOOR=h.prototype.ROUND_FLOOR=3;h.ROUND_HALF_DOWN=h.prototype.ROUND_HALF_DOWN=5;h.ROUND_HALF_EVEN=h.prototype.ROUND_HALF_EVEN=6;h.ROUND_HALF_UP=h.prototype.ROUND_HALF_UP=4;h.ROUND_UNNECESSARY=h.prototype.ROUND_UNNECESSARY=7;
  14. h.ROUND_UP=h.prototype.ROUND_UP=0;h.prototype.DEFAULT_FORM=h.prototype.SCIENTIFIC;h.prototype.DEFAULT_DIGITS=9;h.prototype.DEFAULT_LOSTDIGITS=!1;h.prototype.DEFAULT_ROUNDINGMODE=h.prototype.ROUND_HALF_UP;h.prototype.MIN_DIGITS=0;h.prototype.MAX_DIGITS=999999999;h.prototype.ROUNDS=[h.prototype.ROUND_HALF_UP,h.prototype.ROUND_UNNECESSARY,h.prototype.ROUND_CEILING,h.prototype.ROUND_DOWN,h.prototype.ROUND_FLOOR,h.prototype.ROUND_HALF_DOWN,h.prototype.ROUND_HALF_EVEN,h.prototype.ROUND_UP];h.prototype.ROUNDWORDS=
  15. "ROUND_HALF_UP ROUND_UNNECESSARY ROUND_CEILING ROUND_DOWN ROUND_FLOOR ROUND_HALF_DOWN ROUND_HALF_EVEN ROUND_UP".split(" ");h.prototype.DEFAULT=new h(h.prototype.DEFAULT_DIGITS,h.prototype.DEFAULT_FORM,h.prototype.DEFAULT_LOSTDIGITS,h.prototype.DEFAULT_ROUNDINGMODE);return h}(),L=function(h){function v(a,b){return(a-a%b)/b}function r(a){var b=Array(a),c;for(c=0;c<a;++c)b[c]=0;return b}function f(){this.ind=0;this.form=h.prototype.PLAIN;this.mant=null;this.exp=0;if(0!=f.arguments.length){var a,b,c;
  16. 1==f.arguments.length?(a=f.arguments[0],b=0,c=a.length):(a=f.arguments[0],b=f.arguments[1],c=f.arguments[2]);"string"==typeof a&&(a=a.split(""));var d,e,n,g,k,l=0,m=0;e=!1;var q=m=m=l=0,p=0;g=0;0>=c&&this.bad("BigDecimal(): ",a);this.ind=this.ispos;"-"==a[0]?(c--,0==c&&this.bad("BigDecimal(): ",a),this.ind=this.isneg,b++):"+"==a[0]&&(c--,0==c&&this.bad("BigDecimal(): ",a),b++);e=d=!1;n=0;k=g=-1;q=c;l=b;a:for(;0<q;q--,l++){m=a[l];if("0"<=m&&"9">=m){k=l;n++;continue a}if("."==m){0<=g&&this.bad("BigDecimal(): ",
  17. a);g=l-b;continue a}if("e"!=m&&"E"!=m){("0">m||"9"<m)&&this.bad("BigDecimal(): ",a);d=!0;k=l;n++;continue a}l-b>c-2&&this.bad("BigDecimal(): ",a);e=!1;"-"==a[l+1]?(e=!0,l+=2):l="+"==a[l+1]?l+2:l+1;m=c-(l-b);(0==m||9<m)&&this.bad("BigDecimal(): ",a);c=m;m=l;for(;0<c;c--,m++)q=a[m],"0">q&&this.bad("BigDecimal(): ",a),"9"<q?this.bad("BigDecimal(): ",a):p=q-0,this.exp=10*this.exp+p;e&&(this.exp=-this.exp);e=!0;break a}0==n&&this.bad("BigDecimal(): ",a);0<=g&&(this.exp=this.exp+g-n);p=k-1;l=b;a:for(;l<=
  18. p;l++)if(m=a[l],"0"==m)b++,g--,n--;else if("."==m)b++,g--;else break a;this.mant=Array(n);m=b;if(d)for(b=n,l=0;0<b;b--,l++)l==g&&m++,q=a[m],"9">=q?this.mant[l]=q-0:this.bad("BigDecimal(): ",a),m++;else for(b=n,l=0;0<b;b--,l++)l==g&&m++,this.mant[l]=a[m]-0,m++;0==this.mant[0]?(this.ind=this.iszero,0<this.exp&&(this.exp=0),e&&(this.mant=this.ZERO.mant,this.exp=0)):e&&(this.form=h.prototype.SCIENTIFIC,g=this.exp+this.mant.length-1,(g<this.MinExp||g>this.MaxExp)&&this.bad("BigDecimal(): ",a))}}function t(){var a;
  19. if(1==t.arguments.length)a=t.arguments[0];else if(0==t.arguments.length)a=this.plainMC;else throw"abs(): "+t.arguments.length+" arguments given; expected 0 or 1";return this.ind==this.isneg?this.negate(a):this.plus(a)}function z(){var a;if(2==z.arguments.length)a=z.arguments[1];else if(1==z.arguments.length)a=this.plainMC;else throw"add(): "+z.arguments.length+" arguments given; expected 1 or 2";var b=z.arguments[0],c,d,e,n,g,k,l,m=0;d=m=0;var m=null,q=m=0,p=0,r=0,t=0,s=0;a.lostDigits&&this.checkdigits(b,
  20. a.digits);c=this;if(0==c.ind&&a.form!=h.prototype.PLAIN)return b.plus(a);if(0==b.ind&&a.form!=h.prototype.PLAIN)return c.plus(a);d=a.digits;0<d&&(c.mant.length>d&&(c=this.clone(c).round(a)),b.mant.length>d&&(b=this.clone(b).round(a)));e=new f;n=c.mant;g=c.mant.length;k=b.mant;l=b.mant.length;if(c.exp==b.exp)e.exp=c.exp;else if(c.exp>b.exp){m=g+c.exp-b.exp;if(m>=l+d+1&&0<d)return e.mant=n,e.exp=c.exp,e.ind=c.ind,g<d&&(e.mant=this.extend(c.mant,d),e.exp-=d-g),e.finish(a,!1);e.exp=b.exp;m>d+1&&0<d&&
  21. (m=m-d-1,l-=m,e.exp+=m,m=d+1);m>g&&(g=m)}else{m=l+b.exp-c.exp;if(m>=g+d+1&&0<d)return e.mant=k,e.exp=b.exp,e.ind=b.ind,l<d&&(e.mant=this.extend(b.mant,d),e.exp-=d-l),e.finish(a,!1);e.exp=c.exp;m>d+1&&0<d&&(m=m-d-1,g-=m,e.exp+=m,m=d+1);m>l&&(l=m)}e.ind=c.ind==this.iszero?this.ispos:c.ind;if((c.ind==this.isneg?1:0)==(b.ind==this.isneg?1:0))d=1;else{do{d=-1;do if(b.ind!=this.iszero)if(g<l||c.ind==this.iszero)m=n,n=k,k=m,m=g,g=l,l=m,e.ind=-e.ind;else if(!(g>l))c:for(q=m=0,p=n.length-1,r=k.length-1;;){if(m<=
  22. p)t=n[m];else{if(q>r){if(a.form!=h.prototype.PLAIN)return this.ZERO;break c}t=0}s=q<=r?k[q]:0;if(t!=s){t<s&&(m=n,n=k,k=m,m=g,g=l,l=m,e.ind=-e.ind);break c}m++;q++}while(0)}while(0)}e.mant=this.byteaddsub(n,g,k,l,d,!1);return e.finish(a,!1)}function A(){var a;if(2==A.arguments.length)a=A.arguments[1];else if(1==A.arguments.length)a=this.plainMC;else throw"compareTo(): "+A.arguments.length+" arguments given; expected 1 or 2";var b=A.arguments[0],c=0,c=0;a.lostDigits&&this.checkdigits(b,a.digits);if(this.ind==
  23. b.ind&&this.exp==b.exp){c=this.mant.length;if(c<b.mant.length)return-this.ind;if(c>b.mant.length)return this.ind;if(c<=a.digits||0==a.digits){a=c;c=0;for(;0<a;a--,c++){if(this.mant[c]<b.mant[c])return-this.ind;if(this.mant[c]>b.mant[c])return this.ind}return 0}}else{if(this.ind<b.ind)return-1;if(this.ind>b.ind)return 1}b=this.clone(b);b.ind=-b.ind;return this.add(b,a).ind}function u(){var a,b=-1;if(2==u.arguments.length)a="number"==typeof u.arguments[1]?new h(0,h.prototype.PLAIN,!1,u.arguments[1]):
  24. u.arguments[1];else if(3==u.arguments.length){b=u.arguments[1];if(0>b)throw"divide(): Negative scale: "+b;a=new h(0,h.prototype.PLAIN,!1,u.arguments[2])}else if(1==u.arguments.length)a=this.plainMC;else throw"divide(): "+u.arguments.length+" arguments given; expected between 1 and 3";return this.dodivide("D",u.arguments[0],a,b)}function B(){var a;if(2==B.arguments.length)a=B.arguments[1];else if(1==B.arguments.length)a=this.plainMC;else throw"divideInteger(): "+B.arguments.length+" arguments given; expected 1 or 2";
  25. return this.dodivide("I",B.arguments[0],a,0)}function C(){var a;if(2==C.arguments.length)a=C.arguments[1];else if(1==C.arguments.length)a=this.plainMC;else throw"max(): "+C.arguments.length+" arguments given; expected 1 or 2";var b=C.arguments[0];return 0<=this.compareTo(b,a)?this.plus(a):b.plus(a)}function D(){var a;if(2==D.arguments.length)a=D.arguments[1];else if(1==D.arguments.length)a=this.plainMC;else throw"min(): "+D.arguments.length+" arguments given; expected 1 or 2";var b=D.arguments[0];
  26. return 0>=this.compareTo(b,a)?this.plus(a):b.plus(a)}function E(){var a;if(2==E.arguments.length)a=E.arguments[1];else if(1==E.arguments.length)a=this.plainMC;else throw"multiply(): "+E.arguments.length+" arguments given; expected 1 or 2";var b=E.arguments[0],c,d,e,h=e=null,g,k=0,l,m=0,q=0;a.lostDigits&&this.checkdigits(b,a.digits);c=this;d=0;e=a.digits;0<e?(c.mant.length>e&&(c=this.clone(c).round(a)),b.mant.length>e&&(b=this.clone(b).round(a))):(0<c.exp&&(d+=c.exp),0<b.exp&&(d+=b.exp));c.mant.length<
  27. b.mant.length?(e=c.mant,h=b.mant):(e=b.mant,h=c.mant);g=e.length+h.length-1;k=9<e[0]*h[0]?g+1:g;l=new f;var k=this.createArrayWithZeros(k),p=e.length,m=0;for(;0<p;p--,m++)q=e[m],0!=q&&(k=this.byteaddsub(k,k.length,h,g,q,!0)),g--;l.ind=c.ind*b.ind;l.exp=c.exp+b.exp-d;l.mant=0==d?k:this.extend(k,k.length+d);return l.finish(a,!1)}function J(){var a;if(1==J.arguments.length)a=J.arguments[0];else if(0==J.arguments.length)a=this.plainMC;else throw"negate(): "+J.arguments.length+" arguments given; expected 0 or 1";
  28. var b;a.lostDigits&&this.checkdigits(null,a.digits);b=this.clone(this);b.ind=-b.ind;return b.finish(a,!1)}function K(){var a;if(1==K.arguments.length)a=K.arguments[0];else if(0==K.arguments.length)a=this.plainMC;else throw"plus(): "+K.arguments.length+" arguments given; expected 0 or 1";a.lostDigits&&this.checkdigits(null,a.digits);return a.form==h.prototype.PLAIN&&this.form==h.prototype.PLAIN&&(this.mant.length<=a.digits||0==a.digits)?this:this.clone(this).finish(a,!1)}function F(){var a;if(2==F.arguments.length)a=
  29. F.arguments[1];else if(1==F.arguments.length)a=this.plainMC;else throw"pow(): "+F.arguments.length+" arguments given; expected 1 or 2";var b=F.arguments[0],c,d,e,f=e=0,g,k=0;a.lostDigits&&this.checkdigits(b,a.digits);c=b.intcheck(this.MinArg,this.MaxArg);d=this;e=a.digits;if(0==e){if(b.ind==this.isneg)throw"pow(): Negative power: "+b.toString();e=0}else{if(b.mant.length+b.exp>e)throw"pow(): Too many digits: "+b.toString();d.mant.length>e&&(d=this.clone(d).round(a));f=b.mant.length+b.exp;e=e+f+1}e=
  30. new h(e,a.form,!1,a.roundingMode);f=this.ONE;if(0==c)return f;0>c&&(c=-c);g=!1;k=1;a:for(;;k++){c<<=1;0>c&&(g=!0,f=f.multiply(d,e));if(31==k)break a;if(!g)continue a;f=f.multiply(f,e)}0>b.ind&&(f=this.ONE.divide(f,e));return f.finish(a,!0)}function G(){var a;if(2==G.arguments.length)a=G.arguments[1];else if(1==G.arguments.length)a=this.plainMC;else throw"remainder(): "+G.arguments.length+" arguments given; expected 1 or 2";return this.dodivide("R",G.arguments[0],a,-1)}function H(){var a;if(2==H.arguments.length)a=
  31. H.arguments[1];else if(1==H.arguments.length)a=this.plainMC;else throw"subtract(): "+H.arguments.length+" arguments given; expected 1 or 2";var b=H.arguments[0];a.lostDigits&&this.checkdigits(b,a.digits);b=this.clone(b);b.ind=-b.ind;return this.add(b,a)}function w(){var a,b,c,d;if(6==w.arguments.length)a=w.arguments[2],b=w.arguments[3],c=w.arguments[4],d=w.arguments[5];else if(2==w.arguments.length)b=a=-1,c=h.prototype.SCIENTIFIC,d=this.ROUND_HALF_UP;else throw"format(): "+w.arguments.length+" arguments given; expected 2 or 6";
  32. var e=w.arguments[0],f=w.arguments[1],g,k=0,k=k=0,l=null,m=l=k=0;g=0;k=null;m=l=0;(-1>e||0==e)&&this.badarg("format",1,e);-1>f&&this.badarg("format",2,f);(-1>a||0==a)&&this.badarg("format",3,a);-1>b&&this.badarg("format",4,b);c!=h.prototype.SCIENTIFIC&&c!=h.prototype.ENGINEERING&&(-1==c?c=h.prototype.SCIENTIFIC:this.badarg("format",5,c));if(d!=this.ROUND_HALF_UP)try{-1==d?d=this.ROUND_HALF_UP:new h(9,h.prototype.SCIENTIFIC,!1,d)}catch(q){this.badarg("format",6,d)}g=this.clone(this);-1==b?g.form=h.prototype.PLAIN:
  33. g.ind==this.iszero?g.form=h.prototype.PLAIN:(k=g.exp+g.mant.length,g.form=k>b?c:-5>k?c:h.prototype.PLAIN);if(0<=f)a:for(;;){g.form==h.prototype.PLAIN?k=-g.exp:g.form==h.prototype.SCIENTIFIC?k=g.mant.length-1:(k=(g.exp+g.mant.length-1)%3,0>k&&(k=3+k),k++,k=k>=g.mant.length?0:g.mant.length-k);if(k==f)break a;if(k<f){l=this.extend(g.mant,g.mant.length+f-k);g.mant=l;g.exp-=f-k;if(g.exp<this.MinExp)throw"format(): Exponent Overflow: "+g.exp;break a}k-=f;if(k>g.mant.length){g.mant=this.ZERO.mant;g.ind=
  34. this.iszero;g.exp=0;continue a}l=g.mant.length-k;m=g.exp;g.round(l,d);if(g.exp-m==k)break a}b=g.layout();if(0<e){c=b.length;g=0;a:for(;0<c;c--,g++){if("."==b[g])break a;if("E"==b[g])break a}g>e&&this.badarg("format",1,e);if(g<e){k=Array(b.length+e-g);e-=g;l=0;for(;0<e;e--,l++)k[l]=" ";this.arraycopy(b,0,k,l,b.length);b=k}}if(0<a){e=b.length-1;g=b.length-1;a:for(;0<e;e--,g--)if("E"==b[g])break a;if(0==g){k=Array(b.length+a+2);this.arraycopy(b,0,k,0,b.length);a+=2;l=b.length;for(;0<a;a--,l++)k[l]=" ";
  35. b=k}else if(m=b.length-g-2,m>a&&this.badarg("format",3,a),m<a){k=Array(b.length+a-m);this.arraycopy(b,0,k,0,g+2);a-=m;l=g+2;for(;0<a;a--,l++)k[l]="0";this.arraycopy(b,g+2,k,l,m);b=k}}return b.join("")}function I(){var a;if(2==I.arguments.length)a=I.arguments[1];else if(1==I.arguments.length)a=this.ROUND_UNNECESSARY;else throw"setScale(): "+I.arguments.length+" given; expected 1 or 2";var b=I.arguments[0],c,d;c=c=0;c=this.scale();if(c==b&&this.form==h.prototype.PLAIN)return this;d=this.clone(this);
  36. if(c<=b)c=0==c?d.exp+b:b-c,d.mant=this.extend(d.mant,d.mant.length+c),d.exp=-b;else{if(0>b)throw"setScale(): Negative scale: "+b;c=d.mant.length-(c-b);d=d.round(c,a);d.exp!=-b&&(d.mant=this.extend(d.mant,d.mant.length+1),d.exp-=1)}d.form=h.prototype.PLAIN;return d}function y(){var a,b=0,c=0;a=Array(190);b=0;a:for(;189>=b;b++){c=b-90;if(0<=c){a[b]=c%10;f.prototype.bytecar[b]=v(c,10);continue a}c+=100;a[b]=c%10;f.prototype.bytecar[b]=v(c,10)-10}return a}function x(){var a,b;if(2==x.arguments.length)a=
  37. x.arguments[0],b=x.arguments[1];else if(1==x.arguments.length)b=x.arguments[0],a=b.digits,b=b.roundingMode;else throw"round(): "+x.arguments.length+" arguments given; expected 1 or 2";var c,d,e=!1,f=0,g;c=null;c=this.mant.length-a;if(0>=c)return this;this.exp+=c;c=this.ind;d=this.mant;0<a?(this.mant=Array(a),this.arraycopy(d,0,this.mant,0,a),e=!0,f=d[a]):(this.mant=this.ZERO.mant,this.ind=this.iszero,e=!1,f=0==a?d[0]:0);g=0;if(b==this.ROUND_HALF_UP)5<=f&&(g=c);else if(b==this.ROUND_UNNECESSARY){if(!this.allzero(d,
  38. a))throw"round(): Rounding necessary";}else if(b==this.ROUND_HALF_DOWN)5<f?g=c:5==f&&(this.allzero(d,a+1)||(g=c));else if(b==this.ROUND_HALF_EVEN)5<f?g=c:5==f&&(this.allzero(d,a+1)?1==this.mant[this.mant.length-1]%2&&(g=c):g=c);else if(b!=this.ROUND_DOWN)if(b==this.ROUND_UP)this.allzero(d,a)||(g=c);else if(b==this.ROUND_CEILING)0<c&&(this.allzero(d,a)||(g=c));else if(b==this.ROUND_FLOOR)0>c&&(this.allzero(d,a)||(g=c));else throw"round(): Bad round value: "+b;0!=g&&(this.ind==this.iszero?(this.mant=
  39. this.ONE.mant,this.ind=g):(this.ind==this.isneg&&(g=-g),c=this.byteaddsub(this.mant,this.mant.length,this.ONE.mant,1,g,e),c.length>this.mant.length?(this.exp++,this.arraycopy(c,0,this.mant,0,this.mant.length)):this.mant=c));if(this.exp>this.MaxExp)throw"round(): Exponent Overflow: "+this.exp;return this}f.prototype.div=v;f.prototype.arraycopy=function(a,b,c,d,e){var f;if(d>b)for(f=e-1;0<=f;--f)c[f+d]=a[f+b];else for(f=0;f<e;++f)c[f+d]=a[f+b]};f.prototype.createArrayWithZeros=r;f.prototype.abs=t;f.prototype.add=
  40. z;f.prototype.compareTo=A;f.prototype.divide=u;f.prototype.divideInteger=B;f.prototype.max=C;f.prototype.min=D;f.prototype.multiply=E;f.prototype.negate=J;f.prototype.plus=K;f.prototype.pow=F;f.prototype.remainder=G;f.prototype.subtract=H;f.prototype.equals=function(a){var b=0,c=null,d=null;if(null==a||!(a instanceof f)||this.ind!=a.ind)return!1;if(this.mant.length==a.mant.length&&this.exp==a.exp&&this.form==a.form)for(c=this.mant.length,b=0;0<c;c--,b++){if(this.mant[b]!=a.mant[b])return!1}else{c=
  41. this.layout();d=a.layout();if(c.length!=d.length)return!1;a=c.length;b=0;for(;0<a;a--,b++)if(c[b]!=d[b])return!1}return!0};f.prototype.format=w;f.prototype.intValueExact=function(){var a,b=0,c,d=0;a=0;if(this.ind==this.iszero)return 0;a=this.mant.length-1;if(0>this.exp){a+=this.exp;if(!this.allzero(this.mant,a+1))throw"intValueExact(): Decimal part non-zero: "+this.toString();if(0>a)return 0;b=0}else{if(9<this.exp+a)throw"intValueExact(): Conversion overflow: "+this.toString();b=this.exp}c=0;var e=
  42. a+b,d=0;for(;d<=e;d++)c*=10,d<=a&&(c+=this.mant[d]);if(9==a+b&&(a=v(c,1E9),a!=this.mant[0])){if(-2147483648==c&&this.ind==this.isneg&&2==this.mant[0])return c;throw"intValueExact(): Conversion overflow: "+this.toString();}return this.ind==this.ispos?c:-c};f.prototype.movePointLeft=function(a){var b;b=this.clone(this);b.exp-=a;return b.finish(this.plainMC,!1)};f.prototype.movePointRight=function(a){var b;b=this.clone(this);b.exp+=a;return b.finish(this.plainMC,!1)};f.prototype.scale=function(){return 0<=
  43. this.exp?0:-this.exp};f.prototype.setScale=I;f.prototype.signum=function(){return this.ind};f.prototype.toString=function(){return this.layout().join("")};f.prototype.layout=function(){var a,b=0,b=null,c=0,d=0;a=0;var d=null,e,b=0;a=Array(this.mant.length);c=this.mant.length;b=0;for(;0<c;c--,b++)a[b]=this.mant[b]+"";if(this.form!=h.prototype.PLAIN){b="";this.ind==this.isneg&&(b+="-");c=this.exp+a.length-1;if(this.form==h.prototype.SCIENTIFIC)b+=a[0],1<a.length&&(b+="."),b+=a.slice(1).join("");else if(d=
  44. c%3,0>d&&(d=3+d),c-=d,d++,d>=a.length)for(b+=a.join(""),a=d-a.length;0<a;a--)b+="0";else b+=a.slice(0,d).join(""),b=b+"."+a.slice(d).join("");0!=c&&(0>c?(a="-",c=-c):a="+",b+="E",b+=a,b+=c);return b.split("")}if(0==this.exp){if(0<=this.ind)return a;d=Array(a.length+1);d[0]="-";this.arraycopy(a,0,d,1,a.length);return d}c=this.ind==this.isneg?1:0;e=this.exp+a.length;if(1>e){b=c+2-this.exp;d=Array(b);0!=c&&(d[0]="-");d[c]="0";d[c+1]=".";var f=-e,b=c+2;for(;0<f;f--,b++)d[b]="0";this.arraycopy(a,0,d,c+
  45. 2-e,a.length);return d}if(e>a.length){d=Array(c+e);0!=c&&(d[0]="-");this.arraycopy(a,0,d,c,a.length);e-=a.length;b=c+a.length;for(;0<e;e--,b++)d[b]="0";return d}b=c+1+a.length;d=Array(b);0!=c&&(d[0]="-");this.arraycopy(a,0,d,c,e);d[c+e]=".";this.arraycopy(a,e,d,c+e+1,a.length-e);return d};f.prototype.intcheck=function(a,b){var c;c=this.intValueExact();if(c<a||c>b)throw"intcheck(): Conversion overflow: "+c;return c};f.prototype.dodivide=function(a,b,c,d){var e,n,g,k,l,m,q,p,t,r=0,s=0,u=0;n=n=s=s=s=
  46. 0;e=null;e=e=0;e=null;c.lostDigits&&this.checkdigits(b,c.digits);e=this;if(0==b.ind)throw"dodivide(): Divide by 0";if(0==e.ind)return c.form!=h.prototype.PLAIN?this.ZERO:-1==d?e:e.setScale(d);n=c.digits;0<n?(e.mant.length>n&&(e=this.clone(e).round(c)),b.mant.length>n&&(b=this.clone(b).round(c))):(-1==d&&(d=e.scale()),n=e.mant.length,d!=-e.exp&&(n=n+d+e.exp),n=n-(b.mant.length-1)-b.exp,n<e.mant.length&&(n=e.mant.length),n<b.mant.length&&(n=b.mant.length));g=e.exp-b.exp+e.mant.length-b.mant.length;
  47. if(0>g&&"D"!=a)return"I"==a?this.ZERO:this.clone(e).finish(c,!1);k=new f;k.ind=e.ind*b.ind;k.exp=g;k.mant=this.createArrayWithZeros(n+1);l=n+n+1;g=this.extend(e.mant,l);m=l;q=b.mant;p=l;t=10*q[0]+1;1<q.length&&(t+=q[1]);l=0;a:for(;;){r=0;b:for(;;){if(m<p)break b;if(m==p){c:do{var w=m,s=0;for(;0<w;w--,s++){u=s<q.length?q[s]:0;if(g[s]<u)break b;if(g[s]>u)break c}r++;k.mant[l]=r;l++;g[0]=0;break a}while(0);s=g[0]}else s=10*g[0],1<m&&(s+=g[1]);s=v(10*s,t);0==s&&(s=1);r+=s;g=this.byteaddsub(g,m,q,p,-s,
  48. !0);if(0!=g[0])continue b;u=m-2;s=0;c:for(;s<=u;s++){if(0!=g[s])break c;m--}if(0==s)continue b;this.arraycopy(g,s,g,0,m)}if(0!=l||0!=r){k.mant[l]=r;l++;if(l==n+1)break a;if(0==g[0])break a}if(0<=d&&-k.exp>d)break a;if("D"!=a&&0>=k.exp)break a;k.exp-=1;p--}0==l&&(l=1);if("I"==a||"R"==a){if(l+k.exp>n)throw"dodivide(): Integer overflow";if("R"==a){do{if(0==k.mant[0])return this.clone(e).finish(c,!1);if(0==g[0])return this.ZERO;k.ind=e.ind;n=n+n+1-e.mant.length;k.exp=k.exp-n+e.exp;n=m;s=n-1;b:for(;1<=
  49. s&&k.exp<e.exp&&k.exp<b.exp;s--){if(0!=g[s])break b;n--;k.exp+=1}n<g.length&&(e=Array(n),this.arraycopy(g,0,e,0,n),g=e);k.mant=g;return k.finish(c,!1)}while(0)}}else 0!=g[0]&&(e=k.mant[l-1],0==e%5&&(k.mant[l-1]=e+1));if(0<=d)return l!=k.mant.length&&(k.exp-=k.mant.length-l),e=k.mant.length-(-k.exp-d),k.round(e,c.roundingMode),k.exp!=-d&&(k.mant=this.extend(k.mant,k.mant.length+1),k.exp-=1),k.finish(c,!0);if(l==k.mant.length)k.round(c);else{if(0==k.mant[0])return this.ZERO;e=Array(l);this.arraycopy(k.mant,
  50. 0,e,0,l);k.mant=e}return k.finish(c,!0)};f.prototype.bad=function(a,b){throw a+"Not a number: "+b;};f.prototype.badarg=function(a,b,c){throw"Bad argument "+b+" to "+a+": "+c;};f.prototype.extend=function(a,b){var c;if(a.length==b)return a;c=r(b);this.arraycopy(a,0,c,0,a.length);return c};f.prototype.byteaddsub=function(a,b,c,d,e,f){var g,h,l,m,q,p,r=0;g=p=0;g=a.length;h=c.length;b-=1;m=l=d-1;m<b&&(m=b);d=null;f&&m+1==g&&(d=a);null==d&&(d=this.createArrayWithZeros(m+1));q=!1;1==e?q=!0:-1==e&&(q=!0);
  51. p=0;r=m;a:for(;0<=r;r--){0<=b&&(b<g&&(p+=a[b]),b--);0<=l&&(l<h&&(p=q?0<e?p+c[l]:p-c[l]:p+c[l]*e),l--);if(10>p&&0<=p){do{d[r]=p;p=0;continue a}while(0)}p+=90;d[r]=this.bytedig[p];p=this.bytecar[p]}if(0==p)return d;c=null;f&&m+2==a.length&&(c=a);null==c&&(c=Array(m+2));c[0]=p;a=m+1;g=0;for(;0<a;a--,g++)c[g+1]=d[g];return c};f.prototype.diginit=y;f.prototype.clone=function(a){var b;b=new f;b.ind=a.ind;b.exp=a.exp;b.form=a.form;b.mant=a.mant;return b};f.prototype.checkdigits=function(a,b){if(0!=b){if(this.mant.length>
  52. b&&!this.allzero(this.mant,b))throw"Too many digits: "+this.toString();if(null!=a&&a.mant.length>b&&!this.allzero(a.mant,b))throw"Too many digits: "+a.toString();}};f.prototype.round=x;f.prototype.allzero=function(a,b){var c=0;0>b&&(b=0);var d=a.length-1,c=b;for(;c<=d;c++)if(0!=a[c])return!1;return!0};f.prototype.finish=function(a,b){var c=0,d=0,e=null,c=d=0;0!=a.digits&&this.mant.length>a.digits&&this.round(a);if(b&&a.form!=h.prototype.PLAIN){c=this.mant.length;d=c-1;a:for(;1<=d;d--){if(0!=this.mant[d])break a;
  53. c--;this.exp++}c<this.mant.length&&(e=Array(c),this.arraycopy(this.mant,0,e,0,c),this.mant=e)}this.form=h.prototype.PLAIN;c=this.mant.length;d=0;for(;0<c;c--,d++)if(0!=this.mant[d]){0<d&&(e=Array(this.mant.length-d),this.arraycopy(this.mant,d,e,0,this.mant.length-d),this.mant=e);d=this.exp+this.mant.length;if(0<d){if(d>a.digits&&0!=a.digits&&(this.form=a.form),d-1<=this.MaxExp)return this}else-5>d&&(this.form=a.form);d--;if(d<this.MinExp||d>this.MaxExp){b:do{if(this.form==h.prototype.ENGINEERING&&
  54. (c=d%3,0>c&&(c=3+c),d-=c,d>=this.MinExp&&d<=this.MaxExp))break b;throw"finish(): Exponent Overflow: "+d;}while(0)}return this}this.ind=this.iszero;if(a.form!=h.prototype.PLAIN)this.exp=0;else if(0<this.exp)this.exp=0;else if(this.exp<this.MinExp)throw"finish(): Exponent Overflow: "+this.exp;this.mant=this.ZERO.mant;return this};f.prototype.isGreaterThan=function(a){return 0<this.compareTo(a)};f.prototype.isLessThan=function(a){return 0>this.compareTo(a)};f.prototype.isGreaterThanOrEqualTo=function(a){return 0<=
  55. this.compareTo(a)};f.prototype.isLessThanOrEqualTo=function(a){return 0>=this.compareTo(a)};f.prototype.isPositive=function(){return 0<this.compareTo(f.prototype.ZERO)};f.prototype.isNegative=function(){return 0>this.compareTo(f.prototype.ZERO)};f.prototype.isZero=function(){return 0===this.compareTo(f.prototype.ZERO)};f.ROUND_CEILING=f.prototype.ROUND_CEILING=h.prototype.ROUND_CEILING;f.ROUND_DOWN=f.prototype.ROUND_DOWN=h.prototype.ROUND_DOWN;f.ROUND_FLOOR=f.prototype.ROUND_FLOOR=h.prototype.ROUND_FLOOR;
  56. f.ROUND_HALF_DOWN=f.prototype.ROUND_HALF_DOWN=h.prototype.ROUND_HALF_DOWN;f.ROUND_HALF_EVEN=f.prototype.ROUND_HALF_EVEN=h.prototype.ROUND_HALF_EVEN;f.ROUND_HALF_UP=f.prototype.ROUND_HALF_UP=h.prototype.ROUND_HALF_UP;f.ROUND_UNNECESSARY=f.prototype.ROUND_UNNECESSARY=h.prototype.ROUND_UNNECESSARY;f.ROUND_UP=f.prototype.ROUND_UP=h.prototype.ROUND_UP;f.prototype.ispos=1;f.prototype.iszero=0;f.prototype.isneg=-1;f.prototype.MinExp=-999999999;f.prototype.MaxExp=999999999;f.prototype.MinArg=-999999999;f.prototype.MaxArg=
  57. 999999999;f.prototype.plainMC=new h(0,h.prototype.PLAIN);f.prototype.bytecar=Array(190);f.prototype.bytedig=y();f.ZERO=f.prototype.ZERO=new f("0");f.ONE=f.prototype.ONE=new f("1");f.TEN=f.prototype.TEN=new f("10");return f}(y);"function"===typeof define&&null!=define.amd?define({BigDecimal:L,MathContext:y}):"object"===typeof this&&(this.BigDecimal=L,this.MathContext=y)}).call(this);










声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/423861
推荐阅读
相关标签
  

闽ICP备14008679号