StrictMath.java 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. /* java.lang.StrictMath -- common mathematical functions, strict Java
  2. Copyright (C) 1998, 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. /*
  32. * Some of the algorithms in this class are in the public domain, as part
  33. * of fdlibm (freely-distributable math library), available at
  34. * http://www.netlib.org/fdlibm/, and carry the following copyright:
  35. * ====================================================
  36. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  37. *
  38. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  39. * Permission to use, copy, modify, and distribute this
  40. * software is freely granted, provided that this notice
  41. * is preserved.
  42. * ====================================================
  43. */
  44. package java.lang;
  45. import gnu.classpath.Configuration;
  46. import java.util.Random;
  47. /**
  48. * Helper class containing useful mathematical functions and constants.
  49. * This class mirrors {@link Math}, but is 100% portable, because it uses
  50. * no native methods whatsoever. Also, these algorithms are all accurate
  51. * to less than 1 ulp, and execute in <code>strictfp</code> mode, while
  52. * Math is allowed to vary in its results for some functions. Unfortunately,
  53. * this usually means StrictMath has less efficiency and speed, as Math can
  54. * use native methods.
  55. *
  56. * <p>The source of the various algorithms used is the fdlibm library, at:<br>
  57. * <a href="http://www.netlib.org/fdlibm/">http://www.netlib.org/fdlibm/</a>
  58. *
  59. * Note that angles are specified in radians. Conversion functions are
  60. * provided for your convenience.
  61. *
  62. * @author Eric Blake (ebb9@email.byu.edu)
  63. * @since 1.3
  64. */
  65. public final strictfp class StrictMath
  66. {
  67. /**
  68. * StrictMath is non-instantiable.
  69. */
  70. private StrictMath()
  71. {
  72. }
  73. /**
  74. * A random number generator, initialized on first use.
  75. *
  76. * @see #random()
  77. */
  78. private static Random rand;
  79. /**
  80. * The most accurate approximation to the mathematical constant <em>e</em>:
  81. * <code>2.718281828459045</code>. Used in natural log and exp.
  82. *
  83. * @see #log(double)
  84. * @see #exp(double)
  85. */
  86. public static final double E
  87. = 2.718281828459045; // Long bits 0x4005bf0z8b145769L.
  88. /**
  89. * The most accurate approximation to the mathematical constant <em>pi</em>:
  90. * <code>3.141592653589793</code>. This is the ratio of a circle's diameter
  91. * to its circumference.
  92. */
  93. public static final double PI
  94. = 3.141592653589793; // Long bits 0x400921fb54442d18L.
  95. /**
  96. * Take the absolute value of the argument. (Absolute value means make
  97. * it positive.)
  98. *
  99. * <p>Note that the the largest negative value (Integer.MIN_VALUE) cannot
  100. * be made positive. In this case, because of the rules of negation in
  101. * a computer, MIN_VALUE is what will be returned.
  102. * This is a <em>negative</em> value. You have been warned.
  103. *
  104. * @param i the number to take the absolute value of
  105. * @return the absolute value
  106. * @see Integer#MIN_VALUE
  107. */
  108. public static int abs(int i)
  109. {
  110. return (i < 0) ? -i : i;
  111. }
  112. /**
  113. * Take the absolute value of the argument. (Absolute value means make
  114. * it positive.)
  115. *
  116. * <p>Note that the the largest negative value (Long.MIN_VALUE) cannot
  117. * be made positive. In this case, because of the rules of negation in
  118. * a computer, MIN_VALUE is what will be returned.
  119. * This is a <em>negative</em> value. You have been warned.
  120. *
  121. * @param l the number to take the absolute value of
  122. * @return the absolute value
  123. * @see Long#MIN_VALUE
  124. */
  125. public static long abs(long l)
  126. {
  127. return (l < 0) ? -l : l;
  128. }
  129. /**
  130. * Take the absolute value of the argument. (Absolute value means make
  131. * it positive.)
  132. *
  133. * @param f the number to take the absolute value of
  134. * @return the absolute value
  135. */
  136. public static float abs(float f)
  137. {
  138. return (f <= 0) ? 0 - f : f;
  139. }
  140. /**
  141. * Take the absolute value of the argument. (Absolute value means make
  142. * it positive.)
  143. *
  144. * @param d the number to take the absolute value of
  145. * @return the absolute value
  146. */
  147. public static double abs(double d)
  148. {
  149. return (d <= 0) ? 0 - d : d;
  150. }
  151. /**
  152. * Return whichever argument is smaller.
  153. *
  154. * @param a the first number
  155. * @param b a second number
  156. * @return the smaller of the two numbers
  157. */
  158. public static int min(int a, int b)
  159. {
  160. return (a < b) ? a : b;
  161. }
  162. /**
  163. * Return whichever argument is smaller.
  164. *
  165. * @param a the first number
  166. * @param b a second number
  167. * @return the smaller of the two numbers
  168. */
  169. public static long min(long a, long b)
  170. {
  171. return (a < b) ? a : b;
  172. }
  173. /**
  174. * Return whichever argument is smaller. If either argument is NaN, the
  175. * result is NaN, and when comparing 0 and -0, -0 is always smaller.
  176. *
  177. * @param a the first number
  178. * @param b a second number
  179. * @return the smaller of the two numbers
  180. */
  181. public static float min(float a, float b)
  182. {
  183. // this check for NaN, from JLS 15.21.1, saves a method call
  184. if (a != a)
  185. return a;
  186. // no need to check if b is NaN; < will work correctly
  187. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  188. if (a == 0 && b == 0)
  189. return -(-a - b);
  190. return (a < b) ? a : b;
  191. }
  192. /**
  193. * Return whichever argument is smaller. If either argument is NaN, the
  194. * result is NaN, and when comparing 0 and -0, -0 is always smaller.
  195. *
  196. * @param a the first number
  197. * @param b a second number
  198. * @return the smaller of the two numbers
  199. */
  200. public static double min(double a, double b)
  201. {
  202. // this check for NaN, from JLS 15.21.1, saves a method call
  203. if (a != a)
  204. return a;
  205. // no need to check if b is NaN; < will work correctly
  206. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  207. if (a == 0 && b == 0)
  208. return -(-a - b);
  209. return (a < b) ? a : b;
  210. }
  211. /**
  212. * Return whichever argument is larger.
  213. *
  214. * @param a the first number
  215. * @param b a second number
  216. * @return the larger of the two numbers
  217. */
  218. public static int max(int a, int b)
  219. {
  220. return (a > b) ? a : b;
  221. }
  222. /**
  223. * Return whichever argument is larger.
  224. *
  225. * @param a the first number
  226. * @param b a second number
  227. * @return the larger of the two numbers
  228. */
  229. public static long max(long a, long b)
  230. {
  231. return (a > b) ? a : b;
  232. }
  233. /**
  234. * Return whichever argument is larger. If either argument is NaN, the
  235. * result is NaN, and when comparing 0 and -0, 0 is always larger.
  236. *
  237. * @param a the first number
  238. * @param b a second number
  239. * @return the larger of the two numbers
  240. */
  241. public static float max(float a, float b)
  242. {
  243. // this check for NaN, from JLS 15.21.1, saves a method call
  244. if (a != a)
  245. return a;
  246. // no need to check if b is NaN; > will work correctly
  247. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  248. if (a == 0 && b == 0)
  249. return a - -b;
  250. return (a > b) ? a : b;
  251. }
  252. /**
  253. * Return whichever argument is larger. If either argument is NaN, the
  254. * result is NaN, and when comparing 0 and -0, 0 is always larger.
  255. *
  256. * @param a the first number
  257. * @param b a second number
  258. * @return the larger of the two numbers
  259. */
  260. public static double max(double a, double b)
  261. {
  262. // this check for NaN, from JLS 15.21.1, saves a method call
  263. if (a != a)
  264. return a;
  265. // no need to check if b is NaN; > will work correctly
  266. // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
  267. if (a == 0 && b == 0)
  268. return a - -b;
  269. return (a > b) ? a : b;
  270. }
  271. /**
  272. * The trigonometric function <em>sin</em>. The sine of NaN or infinity is
  273. * NaN, and the sine of 0 retains its sign.
  274. *
  275. * @param a the angle (in radians)
  276. * @return sin(a)
  277. */
  278. public static double sin(double a)
  279. {
  280. if (a == Double.NEGATIVE_INFINITY || ! (a < Double.POSITIVE_INFINITY))
  281. return Double.NaN;
  282. if (abs(a) <= PI / 4)
  283. return sin(a, 0);
  284. // Argument reduction needed.
  285. double[] y = new double[2];
  286. int n = remPiOver2(a, y);
  287. switch (n & 3)
  288. {
  289. case 0:
  290. return sin(y[0], y[1]);
  291. case 1:
  292. return cos(y[0], y[1]);
  293. case 2:
  294. return -sin(y[0], y[1]);
  295. default:
  296. return -cos(y[0], y[1]);
  297. }
  298. }
  299. /**
  300. * The trigonometric function <em>cos</em>. The cosine of NaN or infinity is
  301. * NaN.
  302. *
  303. * @param a the angle (in radians).
  304. * @return cos(a).
  305. */
  306. public static double cos(double a)
  307. {
  308. if (a == Double.NEGATIVE_INFINITY || ! (a < Double.POSITIVE_INFINITY))
  309. return Double.NaN;
  310. if (abs(a) <= PI / 4)
  311. return cos(a, 0);
  312. // Argument reduction needed.
  313. double[] y = new double[2];
  314. int n = remPiOver2(a, y);
  315. switch (n & 3)
  316. {
  317. case 0:
  318. return cos(y[0], y[1]);
  319. case 1:
  320. return -sin(y[0], y[1]);
  321. case 2:
  322. return -cos(y[0], y[1]);
  323. default:
  324. return sin(y[0], y[1]);
  325. }
  326. }
  327. /**
  328. * The trigonometric function <em>tan</em>. The tangent of NaN or infinity
  329. * is NaN, and the tangent of 0 retains its sign.
  330. *
  331. * @param a the angle (in radians)
  332. * @return tan(a)
  333. */
  334. public static double tan(double a)
  335. {
  336. if (a == Double.NEGATIVE_INFINITY || ! (a < Double.POSITIVE_INFINITY))
  337. return Double.NaN;
  338. if (abs(a) <= PI / 4)
  339. return tan(a, 0, false);
  340. // Argument reduction needed.
  341. double[] y = new double[2];
  342. int n = remPiOver2(a, y);
  343. return tan(y[0], y[1], (n & 1) == 1);
  344. }
  345. /**
  346. * The trigonometric function <em>arcsin</em>. The range of angles returned
  347. * is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN or
  348. * its absolute value is beyond 1, the result is NaN; and the arcsine of
  349. * 0 retains its sign.
  350. *
  351. * @param x the sin to turn back into an angle
  352. * @return arcsin(x)
  353. */
  354. public static double asin(double x)
  355. {
  356. boolean negative = x < 0;
  357. if (negative)
  358. x = -x;
  359. if (! (x <= 1))
  360. return Double.NaN;
  361. if (x == 1)
  362. return negative ? -PI / 2 : PI / 2;
  363. if (x < 0.5)
  364. {
  365. if (x < 1 / TWO_27)
  366. return negative ? -x : x;
  367. double t = x * x;
  368. double p = t * (PS0 + t * (PS1 + t * (PS2 + t * (PS3 + t
  369. * (PS4 + t * PS5)))));
  370. double q = 1 + t * (QS1 + t * (QS2 + t * (QS3 + t * QS4)));
  371. return negative ? -x - x * (p / q) : x + x * (p / q);
  372. }
  373. double w = 1 - x; // 1>|x|>=0.5.
  374. double t = w * 0.5;
  375. double p = t * (PS0 + t * (PS1 + t * (PS2 + t * (PS3 + t
  376. * (PS4 + t * PS5)))));
  377. double q = 1 + t * (QS1 + t * (QS2 + t * (QS3 + t * QS4)));
  378. double s = sqrt(t);
  379. if (x >= 0.975)
  380. {
  381. w = p / q;
  382. t = PI / 2 - (2 * (s + s * w) - PI_L / 2);
  383. }
  384. else
  385. {
  386. w = (float) s;
  387. double c = (t - w * w) / (s + w);
  388. p = 2 * s * (p / q) - (PI_L / 2 - 2 * c);
  389. q = PI / 4 - 2 * w;
  390. t = PI / 4 - (p - q);
  391. }
  392. return negative ? -t : t;
  393. }
  394. /**
  395. * The trigonometric function <em>arccos</em>. The range of angles returned
  396. * is 0 to pi radians (0 to 180 degrees). If the argument is NaN or
  397. * its absolute value is beyond 1, the result is NaN.
  398. *
  399. * @param x the cos to turn back into an angle
  400. * @return arccos(x)
  401. */
  402. public static double acos(double x)
  403. {
  404. boolean negative = x < 0;
  405. if (negative)
  406. x = -x;
  407. if (! (x <= 1))
  408. return Double.NaN;
  409. if (x == 1)
  410. return negative ? PI : 0;
  411. if (x < 0.5)
  412. {
  413. if (x < 1 / TWO_57)
  414. return PI / 2;
  415. double z = x * x;
  416. double p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z
  417. * (PS4 + z * PS5)))));
  418. double q = 1 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
  419. double r = x - (PI_L / 2 - x * (p / q));
  420. return negative ? PI / 2 + r : PI / 2 - r;
  421. }
  422. if (negative) // x<=-0.5.
  423. {
  424. double z = (1 + x) * 0.5;
  425. double p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z
  426. * (PS4 + z * PS5)))));
  427. double q = 1 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
  428. double s = sqrt(z);
  429. double w = p / q * s - PI_L / 2;
  430. return PI - 2 * (s + w);
  431. }
  432. double z = (1 - x) * 0.5; // x>0.5.
  433. double s = sqrt(z);
  434. double df = (float) s;
  435. double c = (z - df * df) / (s + df);
  436. double p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z
  437. * (PS4 + z * PS5)))));
  438. double q = 1 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
  439. double w = p / q * s + c;
  440. return 2 * (df + w);
  441. }
  442. /**
  443. * The trigonometric function <em>arcsin</em>. The range of angles returned
  444. * is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN, the
  445. * result is NaN; and the arctangent of 0 retains its sign.
  446. *
  447. * @param x the tan to turn back into an angle
  448. * @return arcsin(x)
  449. * @see #atan2(double, double)
  450. */
  451. public static double atan(double x)
  452. {
  453. double lo;
  454. double hi;
  455. boolean negative = x < 0;
  456. if (negative)
  457. x = -x;
  458. if (x >= TWO_66)
  459. return negative ? -PI / 2 : PI / 2;
  460. if (! (x >= 0.4375)) // |x|<7/16, or NaN.
  461. {
  462. if (! (x >= 1 / TWO_29)) // Small, or NaN.
  463. return negative ? -x : x;
  464. lo = hi = 0;
  465. }
  466. else if (x < 1.1875)
  467. {
  468. if (x < 0.6875) // 7/16<=|x|<11/16.
  469. {
  470. x = (2 * x - 1) / (2 + x);
  471. hi = ATAN_0_5H;
  472. lo = ATAN_0_5L;
  473. }
  474. else // 11/16<=|x|<19/16.
  475. {
  476. x = (x - 1) / (x + 1);
  477. hi = PI / 4;
  478. lo = PI_L / 4;
  479. }
  480. }
  481. else if (x < 2.4375) // 19/16<=|x|<39/16.
  482. {
  483. x = (x - 1.5) / (1 + 1.5 * x);
  484. hi = ATAN_1_5H;
  485. lo = ATAN_1_5L;
  486. }
  487. else // 39/16<=|x|<2**66.
  488. {
  489. x = -1 / x;
  490. hi = PI / 2;
  491. lo = PI_L / 2;
  492. }
  493. // Break sum from i=0 to 10 ATi*z**(i+1) into odd and even poly.
  494. double z = x * x;
  495. double w = z * z;
  496. double s1 = z * (AT0 + w * (AT2 + w * (AT4 + w * (AT6 + w
  497. * (AT8 + w * AT10)))));
  498. double s2 = w * (AT1 + w * (AT3 + w * (AT5 + w * (AT7 + w * AT9))));
  499. if (hi == 0)
  500. return negative ? x * (s1 + s2) - x : x - x * (s1 + s2);
  501. z = hi - ((x * (s1 + s2) - lo) - x);
  502. return negative ? -z : z;
  503. }
  504. /**
  505. * A special version of the trigonometric function <em>arctan</em>, for
  506. * converting rectangular coordinates <em>(x, y)</em> to polar
  507. * <em>(r, theta)</em>. This computes the arctangent of x/y in the range
  508. * of -pi to pi radians (-180 to 180 degrees). Special cases:<ul>
  509. * <li>If either argument is NaN, the result is NaN.</li>
  510. * <li>If the first argument is positive zero and the second argument is
  511. * positive, or the first argument is positive and finite and the second
  512. * argument is positive infinity, then the result is positive zero.</li>
  513. * <li>If the first argument is negative zero and the second argument is
  514. * positive, or the first argument is negative and finite and the second
  515. * argument is positive infinity, then the result is negative zero.</li>
  516. * <li>If the first argument is positive zero and the second argument is
  517. * negative, or the first argument is positive and finite and the second
  518. * argument is negative infinity, then the result is the double value
  519. * closest to pi.</li>
  520. * <li>If the first argument is negative zero and the second argument is
  521. * negative, or the first argument is negative and finite and the second
  522. * argument is negative infinity, then the result is the double value
  523. * closest to -pi.</li>
  524. * <li>If the first argument is positive and the second argument is
  525. * positive zero or negative zero, or the first argument is positive
  526. * infinity and the second argument is finite, then the result is the
  527. * double value closest to pi/2.</li>
  528. * <li>If the first argument is negative and the second argument is
  529. * positive zero or negative zero, or the first argument is negative
  530. * infinity and the second argument is finite, then the result is the
  531. * double value closest to -pi/2.</li>
  532. * <li>If both arguments are positive infinity, then the result is the
  533. * double value closest to pi/4.</li>
  534. * <li>If the first argument is positive infinity and the second argument
  535. * is negative infinity, then the result is the double value closest to
  536. * 3*pi/4.</li>
  537. * <li>If the first argument is negative infinity and the second argument
  538. * is positive infinity, then the result is the double value closest to
  539. * -pi/4.</li>
  540. * <li>If both arguments are negative infinity, then the result is the
  541. * double value closest to -3*pi/4.</li>
  542. *
  543. * </ul><p>This returns theta, the angle of the point. To get r, albeit
  544. * slightly inaccurately, use sqrt(x*x+y*y).
  545. *
  546. * @param y the y position
  547. * @param x the x position
  548. * @return <em>theta</em> in the conversion of (x, y) to (r, theta)
  549. * @see #atan(double)
  550. */
  551. public static double atan2(double y, double x)
  552. {
  553. if (x != x || y != y)
  554. return Double.NaN;
  555. if (x == 1)
  556. return atan(y);
  557. if (x == Double.POSITIVE_INFINITY)
  558. {
  559. if (y == Double.POSITIVE_INFINITY)
  560. return PI / 4;
  561. if (y == Double.NEGATIVE_INFINITY)
  562. return -PI / 4;
  563. return 0 * y;
  564. }
  565. if (x == Double.NEGATIVE_INFINITY)
  566. {
  567. if (y == Double.POSITIVE_INFINITY)
  568. return 3 * PI / 4;
  569. if (y == Double.NEGATIVE_INFINITY)
  570. return -3 * PI / 4;
  571. return (1 / (0 * y) == Double.POSITIVE_INFINITY) ? PI : -PI;
  572. }
  573. if (y == 0)
  574. {
  575. if (1 / (0 * x) == Double.POSITIVE_INFINITY)
  576. return y;
  577. return (1 / y == Double.POSITIVE_INFINITY) ? PI : -PI;
  578. }
  579. if (y == Double.POSITIVE_INFINITY || y == Double.NEGATIVE_INFINITY
  580. || x == 0)
  581. return y < 0 ? -PI / 2 : PI / 2;
  582. double z = abs(y / x); // Safe to do y/x.
  583. if (z > TWO_60)
  584. z = PI / 2 + 0.5 * PI_L;
  585. else if (x < 0 && z < 1 / TWO_60)
  586. z = 0;
  587. else
  588. z = atan(z);
  589. if (x > 0)
  590. return y > 0 ? z : -z;
  591. return y > 0 ? PI - (z - PI_L) : z - PI_L - PI;
  592. }
  593. /**
  594. * Returns the hyperbolic sine of <code>x</code> which is defined as
  595. * (exp(x) - exp(-x)) / 2.
  596. *
  597. * Special cases:
  598. * <ul>
  599. * <li>If the argument is NaN, the result is NaN</li>
  600. * <li>If the argument is positive infinity, the result is positive
  601. * infinity.</li>
  602. * <li>If the argument is negative infinity, the result is negative
  603. * infinity.</li>
  604. * <li>If the argument is zero, the result is zero.</li>
  605. * </ul>
  606. *
  607. * @param x the argument to <em>sinh</em>
  608. * @return the hyperbolic sine of <code>x</code>
  609. *
  610. * @since 1.5
  611. */
  612. public static double sinh(double x)
  613. {
  614. // Method :
  615. // mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
  616. // 1. Replace x by |x| (sinh(-x) = -sinh(x)).
  617. // 2.
  618. // E + E/(E+1)
  619. // 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
  620. // 2
  621. //
  622. // 22 <= x <= lnovft : sinh(x) := exp(x)/2
  623. // lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
  624. // ln2ovft < x : sinh(x) := +inf (overflow)
  625. double t, w, h;
  626. long bits;
  627. long h_bits;
  628. long l_bits;
  629. // handle special cases
  630. if (x != x)
  631. return x;
  632. if (x == Double.POSITIVE_INFINITY)
  633. return Double.POSITIVE_INFINITY;
  634. if (x == Double.NEGATIVE_INFINITY)
  635. return Double.NEGATIVE_INFINITY;
  636. if (x < 0)
  637. h = - 0.5;
  638. else
  639. h = 0.5;
  640. bits = Double.doubleToLongBits(x);
  641. h_bits = getHighDWord(bits) & 0x7fffffffL; // ignore sign
  642. l_bits = getLowDWord(bits);
  643. // |x| in [0, 22], return sign(x) * 0.5 * (E+E/(E+1))
  644. if (h_bits < 0x40360000L) // |x| < 22
  645. {
  646. if (h_bits < 0x3e300000L) // |x| < 2^-28
  647. return x; // for tiny arguments return x
  648. t = expm1(abs(x));
  649. if (h_bits < 0x3ff00000L)
  650. return h * (2.0 * t - t * t / (t + 1.0));
  651. return h * (t + t / (t + 1.0));
  652. }
  653. // |x| in [22, log(Double.MAX_VALUE)], return 0.5 * exp(|x|)
  654. if (h_bits < 0x40862e42L)
  655. return h * exp(abs(x));
  656. // |x| in [log(Double.MAX_VALUE), overflowthreshold]
  657. if ((h_bits < 0x408633ceL)
  658. || ((h_bits == 0x408633ceL) && (l_bits <= 0x8fb9f87dL)))
  659. {
  660. w = exp(0.5 * abs(x));
  661. t = h * w;
  662. return t * w;
  663. }
  664. // |x| > overflowthershold
  665. return h * Double.POSITIVE_INFINITY;
  666. }
  667. /**
  668. * Returns the hyperbolic cosine of <code>x</code>, which is defined as
  669. * (exp(x) + exp(-x)) / 2.
  670. *
  671. * Special cases:
  672. * <ul>
  673. * <li>If the argument is NaN, the result is NaN</li>
  674. * <li>If the argument is positive infinity, the result is positive
  675. * infinity.</li>
  676. * <li>If the argument is negative infinity, the result is positive
  677. * infinity.</li>
  678. * <li>If the argument is zero, the result is one.</li>
  679. * </ul>
  680. *
  681. * @param x the argument to <em>cosh</em>
  682. * @return the hyperbolic cosine of <code>x</code>
  683. *
  684. * @since 1.5
  685. */
  686. public static double cosh(double x)
  687. {
  688. // Method :
  689. // mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
  690. // 1. Replace x by |x| (cosh(x) = cosh(-x)).
  691. // 2.
  692. // [ exp(x) - 1 ]^2
  693. // 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
  694. // 2*exp(x)
  695. //
  696. // exp(x) + 1/exp(x)
  697. // ln2/2 <= x <= 22 : cosh(x) := ------------------
  698. // 2
  699. // 22 <= x <= lnovft : cosh(x) := exp(x)/2
  700. // lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
  701. // ln2ovft < x : cosh(x) := +inf (overflow)
  702. double t, w;
  703. long bits;
  704. long hx;
  705. long lx;
  706. // handle special cases
  707. if (x != x)
  708. return x;
  709. if (x == Double.POSITIVE_INFINITY)
  710. return Double.POSITIVE_INFINITY;
  711. if (x == Double.NEGATIVE_INFINITY)
  712. return Double.POSITIVE_INFINITY;
  713. bits = Double.doubleToLongBits(x);
  714. hx = getHighDWord(bits) & 0x7fffffffL; // ignore sign
  715. lx = getLowDWord(bits);
  716. // |x| in [0, 0.5 * ln(2)], return 1 + expm1(|x|)^2 / (2 * exp(|x|))
  717. if (hx < 0x3fd62e43L)
  718. {
  719. t = expm1(abs(x));
  720. w = 1.0 + t;
  721. // for tiny arguments return 1.
  722. if (hx < 0x3c800000L)
  723. return w;
  724. return 1.0 + (t * t) / (w + w);
  725. }
  726. // |x| in [0.5 * ln(2), 22], return exp(|x|)/2 + 1 / (2 * exp(|x|))
  727. if (hx < 0x40360000L)
  728. {
  729. t = exp(abs(x));
  730. return 0.5 * t + 0.5 / t;
  731. }
  732. // |x| in [22, log(Double.MAX_VALUE)], return 0.5 * exp(|x|)
  733. if (hx < 0x40862e42L)
  734. return 0.5 * exp(abs(x));
  735. // |x| in [log(Double.MAX_VALUE), overflowthreshold],
  736. // return exp(x/2)/2 * exp(x/2)
  737. if ((hx < 0x408633ceL)
  738. || ((hx == 0x408633ceL) && (lx <= 0x8fb9f87dL)))
  739. {
  740. w = exp(0.5 * abs(x));
  741. t = 0.5 * w;
  742. return t * w;
  743. }
  744. // |x| > overflowthreshold
  745. return Double.POSITIVE_INFINITY;
  746. }
  747. /**
  748. * Returns the hyperbolic tangent of <code>x</code>, which is defined as
  749. * (exp(x) - exp(-x)) / (exp(x) + exp(-x)), i.e. sinh(x) / cosh(x).
  750. *
  751. Special cases:
  752. * <ul>
  753. * <li>If the argument is NaN, the result is NaN</li>
  754. * <li>If the argument is positive infinity, the result is 1.</li>
  755. * <li>If the argument is negative infinity, the result is -1.</li>
  756. * <li>If the argument is zero, the result is zero.</li>
  757. * </ul>
  758. *
  759. * @param x the argument to <em>tanh</em>
  760. * @return the hyperbolic tagent of <code>x</code>
  761. *
  762. * @since 1.5
  763. */
  764. public static double tanh(double x)
  765. {
  766. // Method :
  767. // 0. tanh(x) is defined to be (exp(x) - exp(-x)) / (exp(x) + exp(-x))
  768. // 1. reduce x to non-negative by tanh(-x) = -tanh(x).
  769. // 2. 0 <= x <= 2^-55 : tanh(x) := x * (1.0 + x)
  770. // -t
  771. // 2^-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x)
  772. // t + 2
  773. // 2
  774. // 1 <= x <= 22.0 : tanh(x) := 1 - ----- ; t=expm1(2x)
  775. // t + 2
  776. // 22.0 < x <= INF : tanh(x) := 1.
  777. double t, z;
  778. long bits;
  779. long h_bits;
  780. // handle special cases
  781. if (x != x)
  782. return x;
  783. if (x == Double.POSITIVE_INFINITY)
  784. return 1.0;
  785. if (x == Double.NEGATIVE_INFINITY)
  786. return -1.0;
  787. bits = Double.doubleToLongBits(x);
  788. h_bits = getHighDWord(bits) & 0x7fffffffL; // ingnore sign
  789. if (h_bits < 0x40360000L) // |x| < 22
  790. {
  791. if (h_bits < 0x3c800000L) // |x| < 2^-55
  792. return x * (1.0 + x);
  793. if (h_bits >= 0x3ff00000L) // |x| >= 1
  794. {
  795. t = expm1(2.0 * abs(x));
  796. z = 1.0 - 2.0 / (t + 2.0);
  797. }
  798. else // |x| < 1
  799. {
  800. t = expm1(-2.0 * abs(x));
  801. z = -t / (t + 2.0);
  802. }
  803. }
  804. else // |x| >= 22
  805. z = 1.0;
  806. return (x >= 0) ? z : -z;
  807. }
  808. /**
  809. * Returns the lower two words of a long. This is intended to be
  810. * used like this:
  811. * <code>getLowDWord(Double.doubleToLongBits(x))</code>.
  812. */
  813. private static long getLowDWord(long x)
  814. {
  815. return x & 0x00000000ffffffffL;
  816. }
  817. /**
  818. * Returns the higher two words of a long. This is intended to be
  819. * used like this:
  820. * <code>getHighDWord(Double.doubleToLongBits(x))</code>.
  821. */
  822. private static long getHighDWord(long x)
  823. {
  824. return (x & 0xffffffff00000000L) >> 32;
  825. }
  826. /**
  827. * Returns a double with the IEEE754 bit pattern given in the lower
  828. * and higher two words <code>lowDWord</code> and <code>highDWord</code>.
  829. */
  830. private static double buildDouble(long lowDWord, long highDWord)
  831. {
  832. return Double.longBitsToDouble(((highDWord & 0xffffffffL) << 32)
  833. | (lowDWord & 0xffffffffL));
  834. }
  835. /**
  836. * Returns the cube root of <code>x</code>. The sign of the cube root
  837. * is equal to the sign of <code>x</code>.
  838. *
  839. * Special cases:
  840. * <ul>
  841. * <li>If the argument is NaN, the result is NaN</li>
  842. * <li>If the argument is positive infinity, the result is positive
  843. * infinity.</li>
  844. * <li>If the argument is negative infinity, the result is negative
  845. * infinity.</li>
  846. * <li>If the argument is zero, the result is zero with the same
  847. * sign as the argument.</li>
  848. * </ul>
  849. *
  850. * @param x the number to take the cube root of
  851. * @return the cube root of <code>x</code>
  852. * @see #sqrt(double)
  853. *
  854. * @since 1.5
  855. */
  856. public static double cbrt(double x)
  857. {
  858. boolean negative = (x < 0);
  859. double r;
  860. double s;
  861. double t;
  862. double w;
  863. long bits;
  864. long l;
  865. long h;
  866. // handle the special cases
  867. if (x != x)
  868. return x;
  869. if (x == Double.POSITIVE_INFINITY)
  870. return Double.POSITIVE_INFINITY;
  871. if (x == Double.NEGATIVE_INFINITY)
  872. return Double.NEGATIVE_INFINITY;
  873. if (x == 0)
  874. return x;
  875. x = abs(x);
  876. bits = Double.doubleToLongBits(x);
  877. if (bits < 0x0010000000000000L) // subnormal number
  878. {
  879. t = TWO_54;
  880. t *= x;
  881. // __HI(t)=__HI(t)/3+B2;
  882. bits = Double.doubleToLongBits(t);
  883. h = getHighDWord(bits);
  884. l = getLowDWord(bits);
  885. h = h / 3 + CBRT_B2;
  886. t = buildDouble(l, h);
  887. }
  888. else
  889. {
  890. // __HI(t)=__HI(x)/3+B1;
  891. h = getHighDWord(bits);
  892. l = 0;
  893. h = h / 3 + CBRT_B1;
  894. t = buildDouble(l, h);
  895. }
  896. // new cbrt to 23 bits
  897. r = t * t / x;
  898. s = CBRT_C + r * t;
  899. t *= CBRT_G + CBRT_F / (s + CBRT_E + CBRT_D / s);
  900. // chopped to 20 bits and make it larger than cbrt(x)
  901. bits = Double.doubleToLongBits(t);
  902. h = getHighDWord(bits);
  903. // __LO(t)=0;
  904. // __HI(t)+=0x00000001;
  905. l = 0;
  906. h += 1;
  907. t = buildDouble(l, h);
  908. // one step newton iteration to 53 bits with error less than 0.667 ulps
  909. s = t * t; // t * t is exact
  910. r = x / s;
  911. w = t + t;
  912. r = (r - t) / (w + r); // r - t is exact
  913. t = t + t * r;
  914. return negative ? -t : t;
  915. }
  916. /**
  917. * Take <em>e</em><sup>a</sup>. The opposite of <code>log()</code>. If the
  918. * argument is NaN, the result is NaN; if the argument is positive infinity,
  919. * the result is positive infinity; and if the argument is negative
  920. * infinity, the result is positive zero.
  921. *
  922. * @param x the number to raise to the power
  923. * @return the number raised to the power of <em>e</em>
  924. * @see #log(double)
  925. * @see #pow(double, double)
  926. */
  927. public static double exp(double x)
  928. {
  929. if (x != x)
  930. return x;
  931. if (x > EXP_LIMIT_H)
  932. return Double.POSITIVE_INFINITY;
  933. if (x < EXP_LIMIT_L)
  934. return 0;
  935. // Argument reduction.
  936. double hi;
  937. double lo;
  938. int k;
  939. double t = abs(x);
  940. if (t > 0.5 * LN2)
  941. {
  942. if (t < 1.5 * LN2)
  943. {
  944. hi = t - LN2_H;
  945. lo = LN2_L;
  946. k = 1;
  947. }
  948. else
  949. {
  950. k = (int) (INV_LN2 * t + 0.5);
  951. hi = t - k * LN2_H;
  952. lo = k * LN2_L;
  953. }
  954. if (x < 0)
  955. {
  956. hi = -hi;
  957. lo = -lo;
  958. k = -k;
  959. }
  960. x = hi - lo;
  961. }
  962. else if (t < 1 / TWO_28)
  963. return 1;
  964. else
  965. lo = hi = k = 0;
  966. // Now x is in primary range.
  967. t = x * x;
  968. double c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
  969. if (k == 0)
  970. return 1 - (x * c / (c - 2) - x);
  971. double y = 1 - (lo - x * c / (2 - c) - hi);
  972. return scale(y, k);
  973. }
  974. /**
  975. * Returns <em>e</em><sup>x</sup> - 1.
  976. * Special cases:
  977. * <ul>
  978. * <li>If the argument is NaN, the result is NaN.</li>
  979. * <li>If the argument is positive infinity, the result is positive
  980. * infinity</li>
  981. * <li>If the argument is negative infinity, the result is -1.</li>
  982. * <li>If the argument is zero, the result is zero.</li>
  983. * </ul>
  984. *
  985. * @param x the argument to <em>e</em><sup>x</sup> - 1.
  986. * @return <em>e</em> raised to the power <code>x</code> minus one.
  987. * @see #exp(double)
  988. */
  989. public static double expm1(double x)
  990. {
  991. // Method
  992. // 1. Argument reduction:
  993. // Given x, find r and integer k such that
  994. //
  995. // x = k * ln(2) + r, |r| <= 0.5 * ln(2)
  996. //
  997. // Here a correction term c will be computed to compensate
  998. // the error in r when rounded to a floating-point number.
  999. //
  1000. // 2. Approximating expm1(r) by a special rational function on
  1001. // the interval [0, 0.5 * ln(2)]:
  1002. // Since
  1003. // r*(exp(r)+1)/(exp(r)-1) = 2 + r^2/6 - r^4/360 + ...
  1004. // we define R1(r*r) by
  1005. // r*(exp(r)+1)/(exp(r)-1) = 2 + r^2/6 * R1(r*r)
  1006. // That is,
  1007. // R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
  1008. // = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
  1009. // = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
  1010. // We use a special Remes algorithm on [0, 0.347] to generate
  1011. // a polynomial of degree 5 in r*r to approximate R1. The
  1012. // maximum error of this polynomial approximation is bounded
  1013. // by 2**-61. In other words,
  1014. // R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
  1015. // where Q1 = -1.6666666666666567384E-2,
  1016. // Q2 = 3.9682539681370365873E-4,
  1017. // Q3 = -9.9206344733435987357E-6,
  1018. // Q4 = 2.5051361420808517002E-7,
  1019. // Q5 = -6.2843505682382617102E-9;
  1020. // (where z=r*r, and Q1 to Q5 are called EXPM1_Qx in the source)
  1021. // with error bounded by
  1022. // | 5 | -61
  1023. // | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
  1024. // | |
  1025. //
  1026. // expm1(r) = exp(r)-1 is then computed by the following
  1027. // specific way which minimize the accumulation rounding error:
  1028. // 2 3
  1029. // r r [ 3 - (R1 + R1*r/2) ]
  1030. // expm1(r) = r + --- + --- * [--------------------]
  1031. // 2 2 [ 6 - r*(3 - R1*r/2) ]
  1032. //
  1033. // To compensate the error in the argument reduction, we use
  1034. // expm1(r+c) = expm1(r) + c + expm1(r)*c
  1035. // ~ expm1(r) + c + r*c
  1036. // Thus c+r*c will be added in as the correction terms for
  1037. // expm1(r+c). Now rearrange the term to avoid optimization
  1038. // screw up:
  1039. // ( 2 2 )
  1040. // ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
  1041. // expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
  1042. // ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
  1043. // ( )
  1044. //
  1045. // = r - E
  1046. // 3. Scale back to obtain expm1(x):
  1047. // From step 1, we have
  1048. // expm1(x) = either 2^k*[expm1(r)+1] - 1
  1049. // = or 2^k*[expm1(r) + (1-2^-k)]
  1050. // 4. Implementation notes:
  1051. // (A). To save one multiplication, we scale the coefficient Qi
  1052. // to Qi*2^i, and replace z by (x^2)/2.
  1053. // (B). To achieve maximum accuracy, we compute expm1(x) by
  1054. // (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
  1055. // (ii) if k=0, return r-E
  1056. // (iii) if k=-1, return 0.5*(r-E)-0.5
  1057. // (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
  1058. // else return 1.0+2.0*(r-E);
  1059. // (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
  1060. // (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
  1061. // (vii) return 2^k(1-((E+2^-k)-r))
  1062. boolean negative = (x < 0);
  1063. double y, hi, lo, c, t, e, hxs, hfx, r1;
  1064. int k;
  1065. long bits;
  1066. long h_bits;
  1067. long l_bits;
  1068. c = 0.0;
  1069. y = abs(x);
  1070. bits = Double.doubleToLongBits(y);
  1071. h_bits = getHighDWord(bits);
  1072. l_bits = getLowDWord(bits);
  1073. // handle special cases and large arguments
  1074. if (h_bits >= 0x4043687aL) // if |x| >= 56 * ln(2)
  1075. {
  1076. if (h_bits >= 0x40862e42L) // if |x| >= EXP_LIMIT_H
  1077. {
  1078. if (h_bits >= 0x7ff00000L)
  1079. {
  1080. if (((h_bits & 0x000fffffL) | (l_bits & 0xffffffffL)) != 0)
  1081. return x; // exp(NaN) = NaN
  1082. else
  1083. return negative ? -1.0 : x; // exp({+-inf}) = {+inf, -1}
  1084. }
  1085. if (x > EXP_LIMIT_H)
  1086. return Double.POSITIVE_INFINITY; // overflow
  1087. }
  1088. if (negative) // x <= -56 * ln(2)
  1089. return -1.0;
  1090. }
  1091. // argument reduction
  1092. if (h_bits > 0x3fd62e42L) // |x| > 0.5 * ln(2)
  1093. {
  1094. if (h_bits < 0x3ff0a2b2L) // |x| < 1.5 * ln(2)
  1095. {
  1096. if (negative)
  1097. {
  1098. hi = x + LN2_H;
  1099. lo = -LN2_L;
  1100. k = -1;
  1101. }
  1102. else
  1103. {
  1104. hi = x - LN2_H;
  1105. lo = LN2_L;
  1106. k = 1;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. k = (int) (INV_LN2 * x + (negative ? - 0.5 : 0.5));
  1112. t = k;
  1113. hi = x - t * LN2_H;
  1114. lo = t * LN2_L;
  1115. }
  1116. x = hi - lo;
  1117. c = (hi - x) - lo;
  1118. }
  1119. else if (h_bits < 0x3c900000L) // |x| < 2^-54 return x
  1120. return x;
  1121. else
  1122. k = 0;
  1123. // x is now in primary range
  1124. hfx = 0.5 * x;
  1125. hxs = x * hfx;
  1126. r1 = 1.0 + hxs * (EXPM1_Q1
  1127. + hxs * (EXPM1_Q2
  1128. + hxs * (EXPM1_Q3
  1129. + hxs * (EXPM1_Q4
  1130. + hxs * EXPM1_Q5))));
  1131. t = 3.0 - r1 * hfx;
  1132. e = hxs * ((r1 - t) / (6.0 - x * t));
  1133. if (k == 0)
  1134. {
  1135. return x - (x * e - hxs); // c == 0
  1136. }
  1137. else
  1138. {
  1139. e = x * (e - c) - c;
  1140. e -= hxs;
  1141. if (k == -1)
  1142. return 0.5 * (x - e) - 0.5;
  1143. if (k == 1)
  1144. {
  1145. if (x < - 0.25)
  1146. return -2.0 * (e - (x + 0.5));
  1147. else
  1148. return 1.0 + 2.0 * (x - e);
  1149. }
  1150. if (k <= -2 || k > 56) // sufficient to return exp(x) - 1
  1151. {
  1152. y = 1.0 - (e - x);
  1153. bits = Double.doubleToLongBits(y);
  1154. h_bits = getHighDWord(bits);
  1155. l_bits = getLowDWord(bits);
  1156. h_bits += (k << 20); // add k to y's exponent
  1157. y = buildDouble(l_bits, h_bits);
  1158. return y - 1.0;
  1159. }
  1160. t = 1.0;
  1161. if (k < 20)
  1162. {
  1163. bits = Double.doubleToLongBits(t);
  1164. h_bits = 0x3ff00000L - (0x00200000L >> k);
  1165. l_bits = getLowDWord(bits);
  1166. t = buildDouble(l_bits, h_bits); // t = 1 - 2^(-k)
  1167. y = t - (e - x);
  1168. bits = Double.doubleToLongBits(y);
  1169. h_bits = getHighDWord(bits);
  1170. l_bits = getLowDWord(bits);
  1171. h_bits += (k << 20); // add k to y's exponent
  1172. y = buildDouble(l_bits, h_bits);
  1173. }
  1174. else
  1175. {
  1176. bits = Double.doubleToLongBits(t);
  1177. h_bits = (0x000003ffL - k) << 20;
  1178. l_bits = getLowDWord(bits);
  1179. t = buildDouble(l_bits, h_bits); // t = 2^(-k)
  1180. y = x - (e + t);
  1181. y += 1.0;
  1182. bits = Double.doubleToLongBits(y);
  1183. h_bits = getHighDWord(bits);
  1184. l_bits = getLowDWord(bits);
  1185. h_bits += (k << 20); // add k to y's exponent
  1186. y = buildDouble(l_bits, h_bits);
  1187. }
  1188. }
  1189. return y;
  1190. }
  1191. /**
  1192. * Take ln(a) (the natural log). The opposite of <code>exp()</code>. If the
  1193. * argument is NaN or negative, the result is NaN; if the argument is
  1194. * positive infinity, the result is positive infinity; and if the argument
  1195. * is either zero, the result is negative infinity.
  1196. *
  1197. * <p>Note that the way to get log<sub>b</sub>(a) is to do this:
  1198. * <code>ln(a) / ln(b)</code>.
  1199. *
  1200. * @param x the number to take the natural log of
  1201. * @return the natural log of <code>a</code>
  1202. * @see #exp(double)
  1203. */
  1204. public static double log(double x)
  1205. {
  1206. if (x == 0)
  1207. return Double.NEGATIVE_INFINITY;
  1208. if (x < 0)
  1209. return Double.NaN;
  1210. if (! (x < Double.POSITIVE_INFINITY))
  1211. return x;
  1212. // Normalize x.
  1213. long bits = Double.doubleToLongBits(x);
  1214. int exp = (int) (bits >> 52);
  1215. if (exp == 0) // Subnormal x.
  1216. {
  1217. x *= TWO_54;
  1218. bits = Double.doubleToLongBits(x);
  1219. exp = (int) (bits >> 52) - 54;
  1220. }
  1221. exp -= 1023; // Unbias exponent.
  1222. bits = (bits & 0x000fffffffffffffL) | 0x3ff0000000000000L;
  1223. x = Double.longBitsToDouble(bits);
  1224. if (x >= SQRT_2)
  1225. {
  1226. x *= 0.5;
  1227. exp++;
  1228. }
  1229. x--;
  1230. if (abs(x) < 1 / TWO_20)
  1231. {
  1232. if (x == 0)
  1233. return exp * LN2_H + exp * LN2_L;
  1234. double r = x * x * (0.5 - 1 / 3.0 * x);
  1235. if (exp == 0)
  1236. return x - r;
  1237. return exp * LN2_H - ((r - exp * LN2_L) - x);
  1238. }
  1239. double s = x / (2 + x);
  1240. double z = s * s;
  1241. double w = z * z;
  1242. double t1 = w * (LG2 + w * (LG4 + w * LG6));
  1243. double t2 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7)));
  1244. double r = t2 + t1;
  1245. if (bits >= 0x3ff6174a00000000L && bits < 0x3ff6b85200000000L)
  1246. {
  1247. double h = 0.5 * x * x; // Need more accuracy for x near sqrt(2).
  1248. if (exp == 0)
  1249. return x - (h - s * (h + r));
  1250. return exp * LN2_H - ((h - (s * (h + r) + exp * LN2_L)) - x);
  1251. }
  1252. if (exp == 0)
  1253. return x - s * (x - r);
  1254. return exp * LN2_H - ((s * (x - r) - exp * LN2_L) - x);
  1255. }
  1256. /**
  1257. * Take a square root. If the argument is NaN or negative, the result is
  1258. * NaN; if the argument is positive infinity, the result is positive
  1259. * infinity; and if the result is either zero, the result is the same.
  1260. *
  1261. * <p>For other roots, use pow(x, 1/rootNumber).
  1262. *
  1263. * @param x the numeric argument
  1264. * @return the square root of the argument
  1265. * @see #pow(double, double)
  1266. */
  1267. public static double sqrt(double x)
  1268. {
  1269. if (x < 0)
  1270. return Double.NaN;
  1271. if (x == 0 || ! (x < Double.POSITIVE_INFINITY))
  1272. return x;
  1273. // Normalize x.
  1274. long bits = Double.doubleToLongBits(x);
  1275. int exp = (int) (bits >> 52);
  1276. if (exp == 0) // Subnormal x.
  1277. {
  1278. x *= TWO_54;
  1279. bits = Double.doubleToLongBits(x);
  1280. exp = (int) (bits >> 52) - 54;
  1281. }
  1282. exp -= 1023; // Unbias exponent.
  1283. bits = (bits & 0x000fffffffffffffL) | 0x0010000000000000L;
  1284. if ((exp & 1) == 1) // Odd exp, double x to make it even.
  1285. bits <<= 1;
  1286. exp >>= 1;
  1287. // Generate sqrt(x) bit by bit.
  1288. bits <<= 1;
  1289. long q = 0;
  1290. long s = 0;
  1291. long r = 0x0020000000000000L; // Move r right to left.
  1292. while (r != 0)
  1293. {
  1294. long t = s + r;
  1295. if (t <= bits)
  1296. {
  1297. s = t + r;
  1298. bits -= t;
  1299. q += r;
  1300. }
  1301. bits <<= 1;
  1302. r >>= 1;
  1303. }
  1304. // Use floating add to round correctly.
  1305. if (bits != 0)
  1306. q += q & 1;
  1307. return Double.longBitsToDouble((q >> 1) + ((exp + 1022L) << 52));
  1308. }
  1309. /**
  1310. * Raise a number to a power. Special cases:<ul>
  1311. * <li>If the second argument is positive or negative zero, then the result
  1312. * is 1.0.</li>
  1313. * <li>If the second argument is 1.0, then the result is the same as the
  1314. * first argument.</li>
  1315. * <li>If the second argument is NaN, then the result is NaN.</li>
  1316. * <li>If the first argument is NaN and the second argument is nonzero,
  1317. * then the result is NaN.</li>
  1318. * <li>If the absolute value of the first argument is greater than 1 and
  1319. * the second argument is positive infinity, or the absolute value of the
  1320. * first argument is less than 1 and the second argument is negative
  1321. * infinity, then the result is positive infinity.</li>
  1322. * <li>If the absolute value of the first argument is greater than 1 and
  1323. * the second argument is negative infinity, or the absolute value of the
  1324. * first argument is less than 1 and the second argument is positive
  1325. * infinity, then the result is positive zero.</li>
  1326. * <li>If the absolute value of the first argument equals 1 and the second
  1327. * argument is infinite, then the result is NaN.</li>
  1328. * <li>If the first argument is positive zero and the second argument is
  1329. * greater than zero, or the first argument is positive infinity and the
  1330. * second argument is less than zero, then the result is positive zero.</li>
  1331. * <li>If the first argument is positive zero and the second argument is
  1332. * less than zero, or the first argument is positive infinity and the
  1333. * second argument is greater than zero, then the result is positive
  1334. * infinity.</li>
  1335. * <li>If the first argument is negative zero and the second argument is
  1336. * greater than zero but not a finite odd integer, or the first argument is
  1337. * negative infinity and the second argument is less than zero but not a
  1338. * finite odd integer, then the result is positive zero.</li>
  1339. * <li>If the first argument is negative zero and the second argument is a
  1340. * positive finite odd integer, or the first argument is negative infinity
  1341. * and the second argument is a negative finite odd integer, then the result
  1342. * is negative zero.</li>
  1343. * <li>If the first argument is negative zero and the second argument is
  1344. * less than zero but not a finite odd integer, or the first argument is
  1345. * negative infinity and the second argument is greater than zero but not a
  1346. * finite odd integer, then the result is positive infinity.</li>
  1347. * <li>If the first argument is negative zero and the second argument is a
  1348. * negative finite odd integer, or the first argument is negative infinity
  1349. * and the second argument is a positive finite odd integer, then the result
  1350. * is negative infinity.</li>
  1351. * <li>If the first argument is less than zero and the second argument is a
  1352. * finite even integer, then the result is equal to the result of raising
  1353. * the absolute value of the first argument to the power of the second
  1354. * argument.</li>
  1355. * <li>If the first argument is less than zero and the second argument is a
  1356. * finite odd integer, then the result is equal to the negative of the
  1357. * result of raising the absolute value of the first argument to the power
  1358. * of the second argument.</li>
  1359. * <li>If the first argument is finite and less than zero and the second
  1360. * argument is finite and not an integer, then the result is NaN.</li>
  1361. * <li>If both arguments are integers, then the result is exactly equal to
  1362. * the mathematical result of raising the first argument to the power of
  1363. * the second argument if that result can in fact be represented exactly as
  1364. * a double value.</li>
  1365. *
  1366. * </ul><p>(In the foregoing descriptions, a floating-point value is
  1367. * considered to be an integer if and only if it is a fixed point of the
  1368. * method {@link #ceil(double)} or, equivalently, a fixed point of the
  1369. * method {@link #floor(double)}. A value is a fixed point of a one-argument
  1370. * method if and only if the result of applying the method to the value is
  1371. * equal to the value.)
  1372. *
  1373. * @param x the number to raise
  1374. * @param y the power to raise it to
  1375. * @return x<sup>y</sup>
  1376. */
  1377. public static double pow(double x, double y)
  1378. {
  1379. // Special cases first.
  1380. if (y == 0)
  1381. return 1;
  1382. if (y == 1)
  1383. return x;
  1384. if (y == -1)
  1385. return 1 / x;
  1386. if (x != x || y != y)
  1387. return Double.NaN;
  1388. // When x < 0, yisint tells if y is not an integer (0), even(1),
  1389. // or odd (2).
  1390. int yisint = 0;
  1391. if (x < 0 && floor(y) == y)
  1392. yisint = (y % 2 == 0) ? 2 : 1;
  1393. double ax = abs(x);
  1394. double ay = abs(y);
  1395. // More special cases, of y.
  1396. if (ay == Double.POSITIVE_INFINITY)
  1397. {
  1398. if (ax == 1)
  1399. return Double.NaN;
  1400. if (ax > 1)
  1401. return y > 0 ? y : 0;
  1402. return y < 0 ? -y : 0;
  1403. }
  1404. if (y == 2)
  1405. return x * x;
  1406. if (y == 0.5)
  1407. return sqrt(x);
  1408. // More special cases, of x.
  1409. if (x == 0 || ax == Double.POSITIVE_INFINITY || ax == 1)
  1410. {
  1411. if (y < 0)
  1412. ax = 1 / ax;
  1413. if (x < 0)
  1414. {
  1415. if (x == -1 && yisint == 0)
  1416. ax = Double.NaN;
  1417. else if (yisint == 1)
  1418. ax = -ax;
  1419. }
  1420. return ax;
  1421. }
  1422. if (x < 0 && yisint == 0)
  1423. return Double.NaN;
  1424. // Now we can start!
  1425. double t;
  1426. double t1;
  1427. double t2;
  1428. double u;
  1429. double v;
  1430. double w;
  1431. if (ay > TWO_31)
  1432. {
  1433. if (ay > TWO_64) // Automatic over/underflow.
  1434. return ((ax < 1) ? y < 0 : y > 0) ? Double.POSITIVE_INFINITY : 0;
  1435. // Over/underflow if x is not close to one.
  1436. if (ax < 0.9999995231628418)
  1437. return y < 0 ? Double.POSITIVE_INFINITY : 0;
  1438. if (ax >= 1.0000009536743164)
  1439. return y > 0 ? Double.POSITIVE_INFINITY : 0;
  1440. // Now |1-x| is <= 2**-20, sufficient to compute
  1441. // log(x) by x-x^2/2+x^3/3-x^4/4.
  1442. t = x - 1;
  1443. w = t * t * (0.5 - t * (1 / 3.0 - t * 0.25));
  1444. u = INV_LN2_H * t;
  1445. v = t * INV_LN2_L - w * INV_LN2;
  1446. t1 = (float) (u + v);
  1447. t2 = v - (t1 - u);
  1448. }
  1449. else
  1450. {
  1451. long bits = Double.doubleToLongBits(ax);
  1452. int exp = (int) (bits >> 52);
  1453. if (exp == 0) // Subnormal x.
  1454. {
  1455. ax *= TWO_54;
  1456. bits = Double.doubleToLongBits(ax);
  1457. exp = (int) (bits >> 52) - 54;
  1458. }
  1459. exp -= 1023; // Unbias exponent.
  1460. ax = Double.longBitsToDouble((bits & 0x000fffffffffffffL)
  1461. | 0x3ff0000000000000L);
  1462. boolean k;
  1463. if (ax < SQRT_1_5) // |x|<sqrt(3/2).
  1464. k = false;
  1465. else if (ax < SQRT_3) // |x|<sqrt(3).
  1466. k = true;
  1467. else
  1468. {
  1469. k = false;
  1470. ax *= 0.5;
  1471. exp++;
  1472. }
  1473. // Compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5).
  1474. u = ax - (k ? 1.5 : 1);
  1475. v = 1 / (ax + (k ? 1.5 : 1));
  1476. double s = u * v;
  1477. double s_h = (float) s;
  1478. double t_h = (float) (ax + (k ? 1.5 : 1));
  1479. double t_l = ax - (t_h - (k ? 1.5 : 1));
  1480. double s_l = v * ((u - s_h * t_h) - s_h * t_l);
  1481. // Compute log(ax).
  1482. double s2 = s * s;
  1483. double r = s_l * (s_h + s) + s2 * s2
  1484. * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
  1485. s2 = s_h * s_h;
  1486. t_h = (float) (3.0 + s2 + r);
  1487. t_l = r - (t_h - 3.0 - s2);
  1488. // u+v = s*(1+...).
  1489. u = s_h * t_h;
  1490. v = s_l * t_h + t_l * s;
  1491. // 2/(3log2)*(s+...).
  1492. double p_h = (float) (u + v);
  1493. double p_l = v - (p_h - u);
  1494. double z_h = CP_H * p_h;
  1495. double z_l = CP_L * p_h + p_l * CP + (k ? DP_L : 0);
  1496. // log2(ax) = (s+..)*2/(3*log2) = exp + dp_h + z_h + z_l.
  1497. t = exp;
  1498. t1 = (float) (z_h + z_l + (k ? DP_H : 0) + t);
  1499. t2 = z_l - (t1 - t - (k ? DP_H : 0) - z_h);
  1500. }
  1501. // Split up y into y1+y2 and compute (y1+y2)*(t1+t2).
  1502. boolean negative = x < 0 && yisint == 1;
  1503. double y1 = (float) y;
  1504. double p_l = (y - y1) * t1 + y * t2;
  1505. double p_h = y1 * t1;
  1506. double z = p_l + p_h;
  1507. if (z >= 1024) // Detect overflow.
  1508. {
  1509. if (z > 1024 || p_l + OVT > z - p_h)
  1510. return negative ? Double.NEGATIVE_INFINITY
  1511. : Double.POSITIVE_INFINITY;
  1512. }
  1513. else if (z <= -1075) // Detect underflow.
  1514. {
  1515. if (z < -1075 || p_l <= z - p_h)
  1516. return negative ? -0.0 : 0;
  1517. }
  1518. // Compute 2**(p_h+p_l).
  1519. int n = round((float) z);
  1520. p_h -= n;
  1521. t = (float) (p_l + p_h);
  1522. u = t * LN2_H;
  1523. v = (p_l - (t - p_h)) * LN2 + t * LN2_L;
  1524. z = u + v;
  1525. w = v - (z - u);
  1526. t = z * z;
  1527. t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
  1528. double r = (z * t1) / (t1 - 2) - (w + z * w);
  1529. z = scale(1 - (r - z), n);
  1530. return negative ? -z : z;
  1531. }
  1532. /**
  1533. * Get the IEEE 754 floating point remainder on two numbers. This is the
  1534. * value of <code>x - y * <em>n</em></code>, where <em>n</em> is the closest
  1535. * double to <code>x / y</code> (ties go to the even n); for a zero
  1536. * remainder, the sign is that of <code>x</code>. If either argument is NaN,
  1537. * the first argument is infinite, or the second argument is zero, the result
  1538. * is NaN; if x is finite but y is infinite, the result is x.
  1539. *
  1540. * @param x the dividend (the top half)
  1541. * @param y the divisor (the bottom half)
  1542. * @return the IEEE 754-defined floating point remainder of x/y
  1543. * @see #rint(double)
  1544. */
  1545. public static double IEEEremainder(double x, double y)
  1546. {
  1547. // Purge off exception values.
  1548. if (x == Double.NEGATIVE_INFINITY || ! (x < Double.POSITIVE_INFINITY)
  1549. || y == 0 || y != y)
  1550. return Double.NaN;
  1551. boolean negative = x < 0;
  1552. x = abs(x);
  1553. y = abs(y);
  1554. if (x == y || x == 0)
  1555. return 0 * x; // Get correct sign.
  1556. // Achieve x < 2y, then take first shot at remainder.
  1557. if (y < TWO_1023)
  1558. x %= y + y;
  1559. // Now adjust x to get correct precision.
  1560. if (y < 4 / TWO_1023)
  1561. {
  1562. if (x + x > y)
  1563. {
  1564. x -= y;
  1565. if (x + x >= y)
  1566. x -= y;
  1567. }
  1568. }
  1569. else
  1570. {
  1571. y *= 0.5;
  1572. if (x > y)
  1573. {
  1574. x -= y;
  1575. if (x >= y)
  1576. x -= y;
  1577. }
  1578. }
  1579. return negative ? -x : x;
  1580. }
  1581. /**
  1582. * Take the nearest integer that is that is greater than or equal to the
  1583. * argument. If the argument is NaN, infinite, or zero, the result is the
  1584. * same; if the argument is between -1 and 0, the result is negative zero.
  1585. * Note that <code>Math.ceil(x) == -Math.floor(-x)</code>.
  1586. *
  1587. * @param a the value to act upon
  1588. * @return the nearest integer &gt;= <code>a</code>
  1589. */
  1590. public static double ceil(double a)
  1591. {
  1592. return -floor(-a);
  1593. }
  1594. /**
  1595. * Take the nearest integer that is that is less than or equal to the
  1596. * argument. If the argument is NaN, infinite, or zero, the result is the
  1597. * same. Note that <code>Math.ceil(x) == -Math.floor(-x)</code>.
  1598. *
  1599. * @param a the value to act upon
  1600. * @return the nearest integer &lt;= <code>a</code>
  1601. */
  1602. public static double floor(double a)
  1603. {
  1604. double x = abs(a);
  1605. if (! (x < TWO_52) || (long) a == a)
  1606. return a; // No fraction bits; includes NaN and infinity.
  1607. if (x < 1)
  1608. return a >= 0 ? 0 * a : -1; // Worry about signed zero.
  1609. return a < 0 ? (long) a - 1.0 : (long) a; // Cast to long truncates.
  1610. }
  1611. /**
  1612. * Take the nearest integer to the argument. If it is exactly between
  1613. * two integers, the even integer is taken. If the argument is NaN,
  1614. * infinite, or zero, the result is the same.
  1615. *
  1616. * @param a the value to act upon
  1617. * @return the nearest integer to <code>a</code>
  1618. */
  1619. public static double rint(double a)
  1620. {
  1621. double x = abs(a);
  1622. if (! (x < TWO_52))
  1623. return a; // No fraction bits; includes NaN and infinity.
  1624. if (x <= 0.5)
  1625. return 0 * a; // Worry about signed zero.
  1626. if (x % 2 <= 0.5)
  1627. return (long) a; // Catch round down to even.
  1628. return (long) (a + (a < 0 ? -0.5 : 0.5)); // Cast to long truncates.
  1629. }
  1630. /**
  1631. * Take the nearest integer to the argument. This is equivalent to
  1632. * <code>(int) Math.floor(f + 0.5f)</code>. If the argument is NaN, the
  1633. * result is 0; otherwise if the argument is outside the range of int, the
  1634. * result will be Integer.MIN_VALUE or Integer.MAX_VALUE, as appropriate.
  1635. *
  1636. * @param f the argument to round
  1637. * @return the nearest integer to the argument
  1638. * @see Integer#MIN_VALUE
  1639. * @see Integer#MAX_VALUE
  1640. */
  1641. public static int round(float f)
  1642. {
  1643. return (int) floor(f + 0.5f);
  1644. }
  1645. /**
  1646. * Take the nearest long to the argument. This is equivalent to
  1647. * <code>(long) Math.floor(d + 0.5)</code>. If the argument is NaN, the
  1648. * result is 0; otherwise if the argument is outside the range of long, the
  1649. * result will be Long.MIN_VALUE or Long.MAX_VALUE, as appropriate.
  1650. *
  1651. * @param d the argument to round
  1652. * @return the nearest long to the argument
  1653. * @see Long#MIN_VALUE
  1654. * @see Long#MAX_VALUE
  1655. */
  1656. public static long round(double d)
  1657. {
  1658. return (long) floor(d + 0.5);
  1659. }
  1660. /**
  1661. * Get a random number. This behaves like Random.nextDouble(), seeded by
  1662. * System.currentTimeMillis() when first called. In other words, the number
  1663. * is from a pseudorandom sequence, and lies in the range [+0.0, 1.0).
  1664. * This random sequence is only used by this method, and is threadsafe,
  1665. * although you may want your own random number generator if it is shared
  1666. * among threads.
  1667. *
  1668. * @return a random number
  1669. * @see Random#nextDouble()
  1670. * @see System#currentTimeMillis()
  1671. */
  1672. public static synchronized double random()
  1673. {
  1674. if (rand == null)
  1675. rand = new Random();
  1676. return rand.nextDouble();
  1677. }
  1678. /**
  1679. * Convert from degrees to radians. The formula for this is
  1680. * radians = degrees * (pi/180); however it is not always exact given the
  1681. * limitations of floating point numbers.
  1682. *
  1683. * @param degrees an angle in degrees
  1684. * @return the angle in radians
  1685. */
  1686. public static double toRadians(double degrees)
  1687. {
  1688. return (degrees * PI) / 180;
  1689. }
  1690. /**
  1691. * Convert from radians to degrees. The formula for this is
  1692. * degrees = radians * (180/pi); however it is not always exact given the
  1693. * limitations of floating point numbers.
  1694. *
  1695. * @param rads an angle in radians
  1696. * @return the angle in degrees
  1697. */
  1698. public static double toDegrees(double rads)
  1699. {
  1700. return (rads * 180) / PI;
  1701. }
  1702. /**
  1703. * Constants for scaling and comparing doubles by powers of 2. The compiler
  1704. * must automatically inline constructs like (1/TWO_54), so we don't list
  1705. * negative powers of two here.
  1706. */
  1707. private static final double
  1708. TWO_16 = 0x10000, // Long bits 0x40f0000000000000L.
  1709. TWO_20 = 0x100000, // Long bits 0x4130000000000000L.
  1710. TWO_24 = 0x1000000, // Long bits 0x4170000000000000L.
  1711. TWO_27 = 0x8000000, // Long bits 0x41a0000000000000L.
  1712. TWO_28 = 0x10000000, // Long bits 0x41b0000000000000L.
  1713. TWO_29 = 0x20000000, // Long bits 0x41c0000000000000L.
  1714. TWO_31 = 0x80000000L, // Long bits 0x41e0000000000000L.
  1715. TWO_49 = 0x2000000000000L, // Long bits 0x4300000000000000L.
  1716. TWO_52 = 0x10000000000000L, // Long bits 0x4330000000000000L.
  1717. TWO_54 = 0x40000000000000L, // Long bits 0x4350000000000000L.
  1718. TWO_57 = 0x200000000000000L, // Long bits 0x4380000000000000L.
  1719. TWO_60 = 0x1000000000000000L, // Long bits 0x43b0000000000000L.
  1720. TWO_64 = 1.8446744073709552e19, // Long bits 0x43f0000000000000L.
  1721. TWO_66 = 7.378697629483821e19, // Long bits 0x4410000000000000L.
  1722. TWO_1023 = 8.98846567431158e307; // Long bits 0x7fe0000000000000L.
  1723. /**
  1724. * Super precision for 2/pi in 24-bit chunks, for use in
  1725. * {@link #remPiOver2(double, double[])}.
  1726. */
  1727. private static final int TWO_OVER_PI[] = {
  1728. 0xa2f983, 0x6e4e44, 0x1529fc, 0x2757d1, 0xf534dd, 0xc0db62,
  1729. 0x95993c, 0x439041, 0xfe5163, 0xabdebb, 0xc561b7, 0x246e3a,
  1730. 0x424dd2, 0xe00649, 0x2eea09, 0xd1921c, 0xfe1deb, 0x1cb129,
  1731. 0xa73ee8, 0x8235f5, 0x2ebb44, 0x84e99c, 0x7026b4, 0x5f7e41,
  1732. 0x3991d6, 0x398353, 0x39f49c, 0x845f8b, 0xbdf928, 0x3b1ff8,
  1733. 0x97ffde, 0x05980f, 0xef2f11, 0x8b5a0a, 0x6d1f6d, 0x367ecf,
  1734. 0x27cb09, 0xb74f46, 0x3f669e, 0x5fea2d, 0x7527ba, 0xc7ebe5,
  1735. 0xf17b3d, 0x0739f7, 0x8a5292, 0xea6bfb, 0x5fb11f, 0x8d5d08,
  1736. 0x560330, 0x46fc7b, 0x6babf0, 0xcfbc20, 0x9af436, 0x1da9e3,
  1737. 0x91615e, 0xe61b08, 0x659985, 0x5f14a0, 0x68408d, 0xffd880,
  1738. 0x4d7327, 0x310606, 0x1556ca, 0x73a8c9, 0x60e27b, 0xc08c6b,
  1739. };
  1740. /**
  1741. * Super precision for pi/2 in 24-bit chunks, for use in
  1742. * {@link #remPiOver2(double, double[])}.
  1743. */
  1744. private static final double PI_OVER_TWO[] = {
  1745. 1.570796251296997, // Long bits 0x3ff921fb40000000L.
  1746. 7.549789415861596e-8, // Long bits 0x3e74442d00000000L.
  1747. 5.390302529957765e-15, // Long bits 0x3cf8469880000000L.
  1748. 3.282003415807913e-22, // Long bits 0x3b78cc5160000000L.
  1749. 1.270655753080676e-29, // Long bits 0x39f01b8380000000L.
  1750. 1.2293330898111133e-36, // Long bits 0x387a252040000000L.
  1751. 2.7337005381646456e-44, // Long bits 0x36e3822280000000L.
  1752. 2.1674168387780482e-51, // Long bits 0x3569f31d00000000L.
  1753. };
  1754. /**
  1755. * More constants related to pi, used in
  1756. * {@link #remPiOver2(double, double[])} and elsewhere.
  1757. */
  1758. private static final double
  1759. PI_L = 1.2246467991473532e-16, // Long bits 0x3ca1a62633145c07L.
  1760. PIO2_1 = 1.5707963267341256, // Long bits 0x3ff921fb54400000L.
  1761. PIO2_1L = 6.077100506506192e-11, // Long bits 0x3dd0b4611a626331L.
  1762. PIO2_2 = 6.077100506303966e-11, // Long bits 0x3dd0b4611a600000L.
  1763. PIO2_2L = 2.0222662487959506e-21, // Long bits 0x3ba3198a2e037073L.
  1764. PIO2_3 = 2.0222662487111665e-21, // Long bits 0x3ba3198a2e000000L.
  1765. PIO2_3L = 8.4784276603689e-32; // Long bits 0x397b839a252049c1L.
  1766. /**
  1767. * Natural log and square root constants, for calculation of
  1768. * {@link #exp(double)}, {@link #log(double)} and
  1769. * {@link #pow(double, double)}. CP is 2/(3*ln(2)).
  1770. */
  1771. private static final double
  1772. SQRT_1_5 = 1.224744871391589, // Long bits 0x3ff3988e1409212eL.
  1773. SQRT_2 = 1.4142135623730951, // Long bits 0x3ff6a09e667f3bcdL.
  1774. SQRT_3 = 1.7320508075688772, // Long bits 0x3ffbb67ae8584caaL.
  1775. EXP_LIMIT_H = 709.782712893384, // Long bits 0x40862e42fefa39efL.
  1776. EXP_LIMIT_L = -745.1332191019411, // Long bits 0xc0874910d52d3051L.
  1777. CP = 0.9617966939259756, // Long bits 0x3feec709dc3a03fdL.
  1778. CP_H = 0.9617967009544373, // Long bits 0x3feec709e0000000L.
  1779. CP_L = -7.028461650952758e-9, // Long bits 0xbe3e2fe0145b01f5L.
  1780. LN2 = 0.6931471805599453, // Long bits 0x3fe62e42fefa39efL.
  1781. LN2_H = 0.6931471803691238, // Long bits 0x3fe62e42fee00000L.
  1782. LN2_L = 1.9082149292705877e-10, // Long bits 0x3dea39ef35793c76L.
  1783. INV_LN2 = 1.4426950408889634, // Long bits 0x3ff71547652b82feL.
  1784. INV_LN2_H = 1.4426950216293335, // Long bits 0x3ff7154760000000L.
  1785. INV_LN2_L = 1.9259629911266175e-8; // Long bits 0x3e54ae0bf85ddf44L.
  1786. /**
  1787. * Constants for computing {@link #log(double)}.
  1788. */
  1789. private static final double
  1790. LG1 = 0.6666666666666735, // Long bits 0x3fe5555555555593L.
  1791. LG2 = 0.3999999999940942, // Long bits 0x3fd999999997fa04L.
  1792. LG3 = 0.2857142874366239, // Long bits 0x3fd2492494229359L.
  1793. LG4 = 0.22222198432149784, // Long bits 0x3fcc71c51d8e78afL.
  1794. LG5 = 0.1818357216161805, // Long bits 0x3fc7466496cb03deL.
  1795. LG6 = 0.15313837699209373, // Long bits 0x3fc39a09d078c69fL.
  1796. LG7 = 0.14798198605116586; // Long bits 0x3fc2f112df3e5244L.
  1797. /**
  1798. * Constants for computing {@link #pow(double, double)}. L and P are
  1799. * coefficients for series; OVT is -(1024-log2(ovfl+.5ulp)); and DP is ???.
  1800. * The P coefficients also calculate {@link #exp(double)}.
  1801. */
  1802. private static final double
  1803. L1 = 0.5999999999999946, // Long bits 0x3fe3333333333303L.
  1804. L2 = 0.4285714285785502, // Long bits 0x3fdb6db6db6fabffL.
  1805. L3 = 0.33333332981837743, // Long bits 0x3fd55555518f264dL.
  1806. L4 = 0.272728123808534, // Long bits 0x3fd17460a91d4101L.
  1807. L5 = 0.23066074577556175, // Long bits 0x3fcd864a93c9db65L.
  1808. L6 = 0.20697501780033842, // Long bits 0x3fca7e284a454eefL.
  1809. P1 = 0.16666666666666602, // Long bits 0x3fc555555555553eL.
  1810. P2 = -2.7777777777015593e-3, // Long bits 0xbf66c16c16bebd93L.
  1811. P3 = 6.613756321437934e-5, // Long bits 0x3f11566aaf25de2cL.
  1812. P4 = -1.6533902205465252e-6, // Long bits 0xbebbbd41c5d26bf1L.
  1813. P5 = 4.1381367970572385e-8, // Long bits 0x3e66376972bea4d0L.
  1814. DP_H = 0.5849624872207642, // Long bits 0x3fe2b80340000000L.
  1815. DP_L = 1.350039202129749e-8, // Long bits 0x3e4cfdeb43cfd006L.
  1816. OVT = 8.008566259537294e-17; // Long bits 0x3c971547652b82feL.
  1817. /**
  1818. * Coefficients for computing {@link #sin(double)}.
  1819. */
  1820. private static final double
  1821. S1 = -0.16666666666666632, // Long bits 0xbfc5555555555549L.
  1822. S2 = 8.33333333332249e-3, // Long bits 0x3f8111111110f8a6L.
  1823. S3 = -1.984126982985795e-4, // Long bits 0xbf2a01a019c161d5L.
  1824. S4 = 2.7557313707070068e-6, // Long bits 0x3ec71de357b1fe7dL.
  1825. S5 = -2.5050760253406863e-8, // Long bits 0xbe5ae5e68a2b9cebL.
  1826. S6 = 1.58969099521155e-10; // Long bits 0x3de5d93a5acfd57cL.
  1827. /**
  1828. * Coefficients for computing {@link #cos(double)}.
  1829. */
  1830. private static final double
  1831. C1 = 0.0416666666666666, // Long bits 0x3fa555555555554cL.
  1832. C2 = -1.388888888887411e-3, // Long bits 0xbf56c16c16c15177L.
  1833. C3 = 2.480158728947673e-5, // Long bits 0x3efa01a019cb1590L.
  1834. C4 = -2.7557314351390663e-7, // Long bits 0xbe927e4f809c52adL.
  1835. C5 = 2.087572321298175e-9, // Long bits 0x3e21ee9ebdb4b1c4L.
  1836. C6 = -1.1359647557788195e-11; // Long bits 0xbda8fae9be8838d4L.
  1837. /**
  1838. * Coefficients for computing {@link #tan(double)}.
  1839. */
  1840. private static final double
  1841. T0 = 0.3333333333333341, // Long bits 0x3fd5555555555563L.
  1842. T1 = 0.13333333333320124, // Long bits 0x3fc111111110fe7aL.
  1843. T2 = 0.05396825397622605, // Long bits 0x3faba1ba1bb341feL.
  1844. T3 = 0.021869488294859542, // Long bits 0x3f9664f48406d637L.
  1845. T4 = 8.8632398235993e-3, // Long bits 0x3f8226e3e96e8493L.
  1846. T5 = 3.5920791075913124e-3, // Long bits 0x3f6d6d22c9560328L.
  1847. T6 = 1.4562094543252903e-3, // Long bits 0x3f57dbc8fee08315L.
  1848. T7 = 5.880412408202641e-4, // Long bits 0x3f4344d8f2f26501L.
  1849. T8 = 2.464631348184699e-4, // Long bits 0x3f3026f71a8d1068L.
  1850. T9 = 7.817944429395571e-5, // Long bits 0x3f147e88a03792a6L.
  1851. T10 = 7.140724913826082e-5, // Long bits 0x3f12b80f32f0a7e9L.
  1852. T11 = -1.8558637485527546e-5, // Long bits 0xbef375cbdb605373L.
  1853. T12 = 2.590730518636337e-5; // Long bits 0x3efb2a7074bf7ad4L.
  1854. /**
  1855. * Coefficients for computing {@link #asin(double)} and
  1856. * {@link #acos(double)}.
  1857. */
  1858. private static final double
  1859. PS0 = 0.16666666666666666, // Long bits 0x3fc5555555555555L.
  1860. PS1 = -0.3255658186224009, // Long bits 0xbfd4d61203eb6f7dL.
  1861. PS2 = 0.20121253213486293, // Long bits 0x3fc9c1550e884455L.
  1862. PS3 = -0.04005553450067941, // Long bits 0xbfa48228b5688f3bL.
  1863. PS4 = 7.915349942898145e-4, // Long bits 0x3f49efe07501b288L.
  1864. PS5 = 3.479331075960212e-5, // Long bits 0x3f023de10dfdf709L.
  1865. QS1 = -2.403394911734414, // Long bits 0xc0033a271c8a2d4bL.
  1866. QS2 = 2.0209457602335057, // Long bits 0x40002ae59c598ac8L.
  1867. QS3 = -0.6882839716054533, // Long bits 0xbfe6066c1b8d0159L.
  1868. QS4 = 0.07703815055590194; // Long bits 0x3fb3b8c5b12e9282L.
  1869. /**
  1870. * Coefficients for computing {@link #atan(double)}.
  1871. */
  1872. private static final double
  1873. ATAN_0_5H = 0.4636476090008061, // Long bits 0x3fddac670561bb4fL.
  1874. ATAN_0_5L = 2.2698777452961687e-17, // Long bits 0x3c7a2b7f222f65e2L.
  1875. ATAN_1_5H = 0.982793723247329, // Long bits 0x3fef730bd281f69bL.
  1876. ATAN_1_5L = 1.3903311031230998e-17, // Long bits 0x3c7007887af0cbbdL.
  1877. AT0 = 0.3333333333333293, // Long bits 0x3fd555555555550dL.
  1878. AT1 = -0.19999999999876483, // Long bits 0xbfc999999998ebc4L.
  1879. AT2 = 0.14285714272503466, // Long bits 0x3fc24924920083ffL.
  1880. AT3 = -0.11111110405462356, // Long bits 0xbfbc71c6fe231671L.
  1881. AT4 = 0.09090887133436507, // Long bits 0x3fb745cdc54c206eL.
  1882. AT5 = -0.0769187620504483, // Long bits 0xbfb3b0f2af749a6dL.
  1883. AT6 = 0.06661073137387531, // Long bits 0x3fb10d66a0d03d51L.
  1884. AT7 = -0.058335701337905735, // Long bits 0xbfadde2d52defd9aL.
  1885. AT8 = 0.049768779946159324, // Long bits 0x3fa97b4b24760debL.
  1886. AT9 = -0.036531572744216916, // Long bits 0xbfa2b4442c6a6c2fL.
  1887. AT10 = 0.016285820115365782; // Long bits 0x3f90ad3ae322da11L.
  1888. /**
  1889. * Constants for computing {@link #cbrt(double)}.
  1890. */
  1891. private static final int
  1892. CBRT_B1 = 715094163, // B1 = (682-0.03306235651)*2**20
  1893. CBRT_B2 = 696219795; // B2 = (664-0.03306235651)*2**20
  1894. /**
  1895. * Constants for computing {@link #cbrt(double)}.
  1896. */
  1897. private static final double
  1898. CBRT_C = 5.42857142857142815906e-01, // Long bits 0x3fe15f15f15f15f1L
  1899. CBRT_D = -7.05306122448979611050e-01, // Long bits 0xbfe691de2532c834L
  1900. CBRT_E = 1.41428571428571436819e+00, // Long bits 0x3ff6a0ea0ea0ea0fL
  1901. CBRT_F = 1.60714285714285720630e+00, // Long bits 0x3ff9b6db6db6db6eL
  1902. CBRT_G = 3.57142857142857150787e-01; // Long bits 0x3fd6db6db6db6db7L
  1903. /**
  1904. * Constants for computing {@link #expm1(double)}
  1905. */
  1906. private static final double
  1907. EXPM1_Q1 = -3.33333333333331316428e-02, // Long bits 0xbfa11111111110f4L
  1908. EXPM1_Q2 = 1.58730158725481460165e-03, // Long bits 0x3f5a01a019fe5585L
  1909. EXPM1_Q3 = -7.93650757867487942473e-05, // Long bits 0xbf14ce199eaadbb7L
  1910. EXPM1_Q4 = 4.00821782732936239552e-06, // Long bits 0x3ed0cfca86e65239L
  1911. EXPM1_Q5 = -2.01099218183624371326e-07; // Long bits 0xbe8afdb76e09c32dL
  1912. /**
  1913. * Helper function for reducing an angle to a multiple of pi/2 within
  1914. * [-pi/4, pi/4].
  1915. *
  1916. * @param x the angle; not infinity or NaN, and outside pi/4
  1917. * @param y an array of 2 doubles modified to hold the remander x % pi/2
  1918. * @return the quadrant of the result, mod 4: 0: [-pi/4, pi/4],
  1919. * 1: [pi/4, 3*pi/4], 2: [3*pi/4, 5*pi/4], 3: [-3*pi/4, -pi/4]
  1920. */
  1921. private static int remPiOver2(double x, double[] y)
  1922. {
  1923. boolean negative = x < 0;
  1924. x = abs(x);
  1925. double z;
  1926. int n;
  1927. if (Configuration.DEBUG && (x <= PI / 4 || x != x
  1928. || x == Double.POSITIVE_INFINITY))
  1929. throw new InternalError("Assertion failure");
  1930. if (x < 3 * PI / 4) // If |x| is small.
  1931. {
  1932. z = x - PIO2_1;
  1933. if ((float) x != (float) (PI / 2)) // 33+53 bit pi is good enough.
  1934. {
  1935. y[0] = z - PIO2_1L;
  1936. y[1] = z - y[0] - PIO2_1L;
  1937. }
  1938. else // Near pi/2, use 33+33+53 bit pi.
  1939. {
  1940. z -= PIO2_2;
  1941. y[0] = z - PIO2_2L;
  1942. y[1] = z - y[0] - PIO2_2L;
  1943. }
  1944. n = 1;
  1945. }
  1946. else if (x <= TWO_20 * PI / 2) // Medium size.
  1947. {
  1948. n = (int) (2 / PI * x + 0.5);
  1949. z = x - n * PIO2_1;
  1950. double w = n * PIO2_1L; // First round good to 85 bits.
  1951. y[0] = z - w;
  1952. if (n >= 32 || (float) x == (float) (w))
  1953. {
  1954. if (x / y[0] >= TWO_16) // Second iteration, good to 118 bits.
  1955. {
  1956. double t = z;
  1957. w = n * PIO2_2;
  1958. z = t - w;
  1959. w = n * PIO2_2L - (t - z - w);
  1960. y[0] = z - w;
  1961. if (x / y[0] >= TWO_49) // Third iteration, 151 bits accuracy.
  1962. {
  1963. t = z;
  1964. w = n * PIO2_3;
  1965. z = t - w;
  1966. w = n * PIO2_3L - (t - z - w);
  1967. y[0] = z - w;
  1968. }
  1969. }
  1970. }
  1971. y[1] = z - y[0] - w;
  1972. }
  1973. else
  1974. {
  1975. // All other (large) arguments.
  1976. int e0 = (int) (Double.doubleToLongBits(x) >> 52) - 1046;
  1977. z = scale(x, -e0); // e0 = ilogb(z) - 23.
  1978. double[] tx = new double[3];
  1979. for (int i = 0; i < 2; i++)
  1980. {
  1981. tx[i] = (int) z;
  1982. z = (z - tx[i]) * TWO_24;
  1983. }
  1984. tx[2] = z;
  1985. int nx = 2;
  1986. while (tx[nx] == 0)
  1987. nx--;
  1988. n = remPiOver2(tx, y, e0, nx);
  1989. }
  1990. if (negative)
  1991. {
  1992. y[0] = -y[0];
  1993. y[1] = -y[1];
  1994. return -n;
  1995. }
  1996. return n;
  1997. }
  1998. /**
  1999. * Helper function for reducing an angle to a multiple of pi/2 within
  2000. * [-pi/4, pi/4].
  2001. *
  2002. * @param x the positive angle, broken into 24-bit chunks
  2003. * @param y an array of 2 doubles modified to hold the remander x % pi/2
  2004. * @param e0 the exponent of x[0]
  2005. * @param nx the last index used in x
  2006. * @return the quadrant of the result, mod 4: 0: [-pi/4, pi/4],
  2007. * 1: [pi/4, 3*pi/4], 2: [3*pi/4, 5*pi/4], 3: [-3*pi/4, -pi/4]
  2008. */
  2009. private static int remPiOver2(double[] x, double[] y, int e0, int nx)
  2010. {
  2011. int i;
  2012. int ih;
  2013. int n;
  2014. double fw;
  2015. double z;
  2016. int[] iq = new int[20];
  2017. double[] f = new double[20];
  2018. double[] q = new double[20];
  2019. boolean recompute = false;
  2020. // Initialize jk, jz, jv, q0; note that 3>q0.
  2021. int jk = 4;
  2022. int jz = jk;
  2023. int jv = max((e0 - 3) / 24, 0);
  2024. int q0 = e0 - 24 * (jv + 1);
  2025. // Set up f[0] to f[nx+jk] where f[nx+jk] = TWO_OVER_PI[jv+jk].
  2026. int j = jv - nx;
  2027. int m = nx + jk;
  2028. for (i = 0; i <= m; i++, j++)
  2029. f[i] = (j < 0) ? 0 : TWO_OVER_PI[j];
  2030. // Compute q[0],q[1],...q[jk].
  2031. for (i = 0; i <= jk; i++)
  2032. {
  2033. for (j = 0, fw = 0; j <= nx; j++)
  2034. fw += x[j] * f[nx + i - j];
  2035. q[i] = fw;
  2036. }
  2037. do
  2038. {
  2039. // Distill q[] into iq[] reversingly.
  2040. for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--)
  2041. {
  2042. fw = (int) (1 / TWO_24 * z);
  2043. iq[i] = (int) (z - TWO_24 * fw);
  2044. z = q[j - 1] + fw;
  2045. }
  2046. // Compute n.
  2047. z = scale(z, q0);
  2048. z -= 8 * floor(z * 0.125); // Trim off integer >= 8.
  2049. n = (int) z;
  2050. z -= n;
  2051. ih = 0;
  2052. if (q0 > 0) // Need iq[jz-1] to determine n.
  2053. {
  2054. i = iq[jz - 1] >> (24 - q0);
  2055. n += i;
  2056. iq[jz - 1] -= i << (24 - q0);
  2057. ih = iq[jz - 1] >> (23 - q0);
  2058. }
  2059. else if (q0 == 0)
  2060. ih = iq[jz - 1] >> 23;
  2061. else if (z >= 0.5)
  2062. ih = 2;
  2063. if (ih > 0) // If q > 0.5.
  2064. {
  2065. n += 1;
  2066. int carry = 0;
  2067. for (i = 0; i < jz; i++) // Compute 1-q.
  2068. {
  2069. j = iq[i];
  2070. if (carry == 0)
  2071. {
  2072. if (j != 0)
  2073. {
  2074. carry = 1;
  2075. iq[i] = 0x1000000 - j;
  2076. }
  2077. }
  2078. else
  2079. iq[i] = 0xffffff - j;
  2080. }
  2081. switch (q0)
  2082. {
  2083. case 1: // Rare case: chance is 1 in 12 for non-default.
  2084. iq[jz - 1] &= 0x7fffff;
  2085. break;
  2086. case 2:
  2087. iq[jz - 1] &= 0x3fffff;
  2088. }
  2089. if (ih == 2)
  2090. {
  2091. z = 1 - z;
  2092. if (carry != 0)
  2093. z -= scale(1, q0);
  2094. }
  2095. }
  2096. // Check if recomputation is needed.
  2097. if (z == 0)
  2098. {
  2099. j = 0;
  2100. for (i = jz - 1; i >= jk; i--)
  2101. j |= iq[i];
  2102. if (j == 0) // Need recomputation.
  2103. {
  2104. int k; // k = no. of terms needed.
  2105. for (k = 1; iq[jk - k] == 0; k++)
  2106. ;
  2107. for (i = jz + 1; i <= jz + k; i++) // Add q[jz+1] to q[jz+k].
  2108. {
  2109. f[nx + i] = TWO_OVER_PI[jv + i];
  2110. for (j = 0, fw = 0; j <= nx; j++)
  2111. fw += x[j] * f[nx + i - j];
  2112. q[i] = fw;
  2113. }
  2114. jz += k;
  2115. recompute = true;
  2116. }
  2117. }
  2118. }
  2119. while (recompute);
  2120. // Chop off zero terms.
  2121. if (z == 0)
  2122. {
  2123. jz--;
  2124. q0 -= 24;
  2125. while (iq[jz] == 0)
  2126. {
  2127. jz--;
  2128. q0 -= 24;
  2129. }
  2130. }
  2131. else // Break z into 24-bit if necessary.
  2132. {
  2133. z = scale(z, -q0);
  2134. if (z >= TWO_24)
  2135. {
  2136. fw = (int) (1 / TWO_24 * z);
  2137. iq[jz] = (int) (z - TWO_24 * fw);
  2138. jz++;
  2139. q0 += 24;
  2140. iq[jz] = (int) fw;
  2141. }
  2142. else
  2143. iq[jz] = (int) z;
  2144. }
  2145. // Convert integer "bit" chunk to floating-point value.
  2146. fw = scale(1, q0);
  2147. for (i = jz; i >= 0; i--)
  2148. {
  2149. q[i] = fw * iq[i];
  2150. fw *= 1 / TWO_24;
  2151. }
  2152. // Compute PI_OVER_TWO[0,...,jk]*q[jz,...,0].
  2153. double[] fq = new double[20];
  2154. for (i = jz; i >= 0; i--)
  2155. {
  2156. fw = 0;
  2157. for (int k = 0; k <= jk && k <= jz - i; k++)
  2158. fw += PI_OVER_TWO[k] * q[i + k];
  2159. fq[jz - i] = fw;
  2160. }
  2161. // Compress fq[] into y[].
  2162. fw = 0;
  2163. for (i = jz; i >= 0; i--)
  2164. fw += fq[i];
  2165. y[0] = (ih == 0) ? fw : -fw;
  2166. fw = fq[0] - fw;
  2167. for (i = 1; i <= jz; i++)
  2168. fw += fq[i];
  2169. y[1] = (ih == 0) ? fw : -fw;
  2170. return n;
  2171. }
  2172. /**
  2173. * Helper method for scaling a double by a power of 2.
  2174. *
  2175. * @param x the double
  2176. * @param n the scale; |n| < 2048
  2177. * @return x * 2**n
  2178. */
  2179. private static double scale(double x, int n)
  2180. {
  2181. if (Configuration.DEBUG && abs(n) >= 2048)
  2182. throw new InternalError("Assertion failure");
  2183. if (x == 0 || x == Double.NEGATIVE_INFINITY
  2184. || ! (x < Double.POSITIVE_INFINITY) || n == 0)
  2185. return x;
  2186. long bits = Double.doubleToLongBits(x);
  2187. int exp = (int) (bits >> 52) & 0x7ff;
  2188. if (exp == 0) // Subnormal x.
  2189. {
  2190. x *= TWO_54;
  2191. exp = ((int) (Double.doubleToLongBits(x) >> 52) & 0x7ff) - 54;
  2192. }
  2193. exp += n;
  2194. if (exp > 0x7fe) // Overflow.
  2195. return Double.POSITIVE_INFINITY * x;
  2196. if (exp > 0) // Normal.
  2197. return Double.longBitsToDouble((bits & 0x800fffffffffffffL)
  2198. | ((long) exp << 52));
  2199. if (exp <= -54)
  2200. return 0 * x; // Underflow.
  2201. exp += 54; // Subnormal result.
  2202. x = Double.longBitsToDouble((bits & 0x800fffffffffffffL)
  2203. | ((long) exp << 52));
  2204. return x * (1 / TWO_54);
  2205. }
  2206. /**
  2207. * Helper trig function; computes sin in range [-pi/4, pi/4].
  2208. *
  2209. * @param x angle within about pi/4
  2210. * @param y tail of x, created by remPiOver2
  2211. * @return sin(x+y)
  2212. */
  2213. private static double sin(double x, double y)
  2214. {
  2215. if (Configuration.DEBUG && abs(x + y) > 0.7854)
  2216. throw new InternalError("Assertion failure");
  2217. if (abs(x) < 1 / TWO_27)
  2218. return x; // If |x| ~< 2**-27, already know answer.
  2219. double z = x * x;
  2220. double v = z * x;
  2221. double r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
  2222. if (y == 0)
  2223. return x + v * (S1 + z * r);
  2224. return x - ((z * (0.5 * y - v * r) - y) - v * S1);
  2225. }
  2226. /**
  2227. * Helper trig function; computes cos in range [-pi/4, pi/4].
  2228. *
  2229. * @param x angle within about pi/4
  2230. * @param y tail of x, created by remPiOver2
  2231. * @return cos(x+y)
  2232. */
  2233. private static double cos(double x, double y)
  2234. {
  2235. if (Configuration.DEBUG && abs(x + y) > 0.7854)
  2236. throw new InternalError("Assertion failure");
  2237. x = abs(x);
  2238. if (x < 1 / TWO_27)
  2239. return 1; // If |x| ~< 2**-27, already know answer.
  2240. double z = x * x;
  2241. double r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
  2242. if (x < 0.3)
  2243. return 1 - (0.5 * z - (z * r - x * y));
  2244. double qx = (x > 0.78125) ? 0.28125 : (x * 0.25);
  2245. return 1 - qx - ((0.5 * z - qx) - (z * r - x * y));
  2246. }
  2247. /**
  2248. * Helper trig function; computes tan in range [-pi/4, pi/4].
  2249. *
  2250. * @param x angle within about pi/4
  2251. * @param y tail of x, created by remPiOver2
  2252. * @param invert true iff -1/tan should be returned instead
  2253. * @return tan(x+y)
  2254. */
  2255. private static double tan(double x, double y, boolean invert)
  2256. {
  2257. // PI/2 is irrational, so no double is a perfect multiple of it.
  2258. if (Configuration.DEBUG && (abs(x + y) > 0.7854 || (x == 0 && invert)))
  2259. throw new InternalError("Assertion failure");
  2260. boolean negative = x < 0;
  2261. if (negative)
  2262. {
  2263. x = -x;
  2264. y = -y;
  2265. }
  2266. if (x < 1 / TWO_28) // If |x| ~< 2**-28, already know answer.
  2267. return (negative ? -1 : 1) * (invert ? -1 / x : x);
  2268. double z;
  2269. double w;
  2270. boolean large = x >= 0.6744;
  2271. if (large)
  2272. {
  2273. z = PI / 4 - x;
  2274. w = PI_L / 4 - y;
  2275. x = z + w;
  2276. y = 0;
  2277. }
  2278. z = x * x;
  2279. w = z * z;
  2280. // Break x**5*(T1+x**2*T2+...) into
  2281. // x**5(T1+x**4*T3+...+x**20*T11)
  2282. // + x**5(x**2*(T2+x**4*T4+...+x**22*T12)).
  2283. double r = T1 + w * (T3 + w * (T5 + w * (T7 + w * (T9 + w * T11))));
  2284. double v = z * (T2 + w * (T4 + w * (T6 + w * (T8 + w * (T10 + w * T12)))));
  2285. double s = z * x;
  2286. r = y + z * (s * (r + v) + y);
  2287. r += T0 * s;
  2288. w = x + r;
  2289. if (large)
  2290. {
  2291. v = invert ? -1 : 1;
  2292. return (negative ? -1 : 1) * (v - 2 * (x - (w * w / (w + v) - r)));
  2293. }
  2294. if (! invert)
  2295. return w;
  2296. // Compute -1.0/(x+r) accurately.
  2297. z = (float) w;
  2298. v = r - (z - x);
  2299. double a = -1 / w;
  2300. double t = (float) a;
  2301. return t + a * (1 + t * z + t * v);
  2302. }
  2303. /**
  2304. * <p>
  2305. * Returns the sign of the argument as follows:
  2306. * </p>
  2307. * <ul>
  2308. * <li>If <code>a</code> is greater than zero, the result is 1.0.</li>
  2309. * <li>If <code>a</code> is less than zero, the result is -1.0.</li>
  2310. * <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
  2311. * <li>If <code>a</code> is positive or negative zero, the result is the
  2312. * same.</li>
  2313. * </ul>
  2314. *
  2315. * @param a the numeric argument.
  2316. * @return the sign of the argument.
  2317. * @since 1.5.
  2318. */
  2319. public static double signum(double a)
  2320. {
  2321. // There's no difference.
  2322. return Math.signum(a);
  2323. }
  2324. /**
  2325. * <p>
  2326. * Returns the sign of the argument as follows:
  2327. * </p>
  2328. * <ul>
  2329. * <li>If <code>a</code> is greater than zero, the result is 1.0f.</li>
  2330. * <li>If <code>a</code> is less than zero, the result is -1.0f.</li>
  2331. * <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
  2332. * <li>If <code>a</code> is positive or negative zero, the result is the
  2333. * same.</li>
  2334. * </ul>
  2335. *
  2336. * @param a the numeric argument.
  2337. * @return the sign of the argument.
  2338. * @since 1.5.
  2339. */
  2340. public static float signum(float a)
  2341. {
  2342. // There's no difference.
  2343. return Math.signum(a);
  2344. }
  2345. /**
  2346. * Return the ulp for the given double argument. The ulp is the
  2347. * difference between the argument and the next larger double. Note
  2348. * that the sign of the double argument is ignored, that is,
  2349. * ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned.
  2350. * If the argument is an infinity, then +Inf is returned. If the
  2351. * argument is zero (either positive or negative), then
  2352. * {@link Double#MIN_VALUE} is returned.
  2353. * @param d the double whose ulp should be returned
  2354. * @return the difference between the argument and the next larger double
  2355. * @since 1.5
  2356. */
  2357. public static double ulp(double d)
  2358. {
  2359. // There's no difference.
  2360. return Math.ulp(d);
  2361. }
  2362. /**
  2363. * Return the ulp for the given float argument. The ulp is the
  2364. * difference between the argument and the next larger float. Note
  2365. * that the sign of the float argument is ignored, that is,
  2366. * ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned.
  2367. * If the argument is an infinity, then +Inf is returned. If the
  2368. * argument is zero (either positive or negative), then
  2369. * {@link Float#MIN_VALUE} is returned.
  2370. * @param f the float whose ulp should be returned
  2371. * @return the difference between the argument and the next larger float
  2372. * @since 1.5
  2373. */
  2374. public static float ulp(float f)
  2375. {
  2376. // There's no difference.
  2377. return Math.ulp(f);
  2378. }
  2379. }