You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1344 lines
32 KiB

  1. /**CFile***********************************************************************
  2. FileName [epd.c]
  3. PackageName [epd]
  4. Synopsis [Arithmetic functions with extended double precision.]
  5. Description []
  6. SeeAlso []
  7. Author [In-Ho Moon]
  8. Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. Neither the name of the University of Colorado nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. POSSIBILITY OF SUCH DAMAGE.]
  33. Revision [$Id: epd.c,v 1.10 2004/08/13 18:20:30 fabio Exp $]
  34. ******************************************************************************/
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <math.h>
  39. #include "util.h"
  40. #include "epd.h"
  41. /**Function********************************************************************
  42. Synopsis [Allocates an EpDouble struct.]
  43. Description [Allocates an EpDouble struct.]
  44. SideEffects []
  45. SeeAlso []
  46. ******************************************************************************/
  47. EpDouble *
  48. EpdAlloc(void)
  49. {
  50. EpDouble *epd;
  51. epd = ALLOC(EpDouble, 1);
  52. return(epd);
  53. }
  54. /**Function********************************************************************
  55. Synopsis [Compares two EpDouble struct.]
  56. Description [Compares two EpDouble struct.]
  57. SideEffects []
  58. SeeAlso []
  59. ******************************************************************************/
  60. int
  61. EpdCmp(const char *key1, const char *key2)
  62. {
  63. EpDouble *epd1 = (EpDouble *) key1;
  64. EpDouble *epd2 = (EpDouble *) key2;
  65. if (epd1->type.value != epd2->type.value ||
  66. epd1->exponent != epd2->exponent) {
  67. return(1);
  68. }
  69. return(0);
  70. }
  71. /**Function********************************************************************
  72. Synopsis [Frees an EpDouble struct.]
  73. Description [Frees an EpDouble struct.]
  74. SideEffects []
  75. SeeAlso []
  76. ******************************************************************************/
  77. void
  78. EpdFree(EpDouble *epd)
  79. {
  80. FREE(epd);
  81. }
  82. /**Function********************************************************************
  83. Synopsis [Converts an arbitrary precision double value to a string.]
  84. Description [Converts an arbitrary precision double value to a string.]
  85. SideEffects []
  86. SeeAlso []
  87. ******************************************************************************/
  88. void
  89. EpdGetString(EpDouble *epd, char *str)
  90. {
  91. double value;
  92. int exponent;
  93. char *pos;
  94. if (IsNanDouble(epd->type.value)) {
  95. sprintf(str, "NaN");
  96. return;
  97. } else if (IsInfDouble(epd->type.value)) {
  98. if (epd->type.bits.sign == 1)
  99. sprintf(str, "-Inf");
  100. else
  101. sprintf(str, "Inf");
  102. return;
  103. }
  104. assert(epd->type.bits.exponent == EPD_MAX_BIN ||
  105. epd->type.bits.exponent == 0);
  106. EpdGetValueAndDecimalExponent(epd, &value, &exponent);
  107. sprintf(str, "%e", value);
  108. pos = strstr(str, "e");
  109. if (exponent >= 0) {
  110. if (exponent < 10)
  111. sprintf(pos + 1, "+0%d", exponent);
  112. else
  113. sprintf(pos + 1, "+%d", exponent);
  114. } else {
  115. exponent *= -1;
  116. if (exponent < 10)
  117. sprintf(pos + 1, "-0%d", exponent);
  118. else
  119. sprintf(pos + 1, "-%d", exponent);
  120. }
  121. }
  122. /**Function********************************************************************
  123. Synopsis [Converts double to EpDouble struct.]
  124. Description [Converts double to EpDouble struct.]
  125. SideEffects []
  126. SeeAlso []
  127. ******************************************************************************/
  128. void
  129. EpdConvert(double value, EpDouble *epd)
  130. {
  131. epd->type.value = value;
  132. epd->exponent = 0;
  133. EpdNormalize(epd);
  134. }
  135. /**Function********************************************************************
  136. Synopsis [Multiplies two arbitrary precision double values.]
  137. Description [Multiplies two arbitrary precision double values.]
  138. SideEffects []
  139. SeeAlso []
  140. ******************************************************************************/
  141. void
  142. EpdMultiply(EpDouble *epd1, double value)
  143. {
  144. EpDouble epd2;
  145. double tmp;
  146. int exponent;
  147. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  148. EpdMakeNan(epd1);
  149. return;
  150. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  151. int sign;
  152. EpdConvert(value, &epd2);
  153. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  154. EpdMakeInf(epd1, sign);
  155. return;
  156. }
  157. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  158. EpdConvert(value, &epd2);
  159. tmp = epd1->type.value * epd2.type.value;
  160. exponent = epd1->exponent + epd2.exponent;
  161. epd1->type.value = tmp;
  162. epd1->exponent = exponent;
  163. EpdNormalize(epd1);
  164. }
  165. /**Function********************************************************************
  166. Synopsis [Multiplies two arbitrary precision double values.]
  167. Description [Multiplies two arbitrary precision double values.]
  168. SideEffects []
  169. SeeAlso []
  170. ******************************************************************************/
  171. void
  172. EpdMultiply2(EpDouble *epd1, EpDouble *epd2)
  173. {
  174. double value;
  175. int exponent;
  176. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  177. EpdMakeNan(epd1);
  178. return;
  179. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  180. int sign;
  181. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  182. EpdMakeInf(epd1, sign);
  183. return;
  184. }
  185. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  186. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  187. value = epd1->type.value * epd2->type.value;
  188. exponent = epd1->exponent + epd2->exponent;
  189. epd1->type.value = value;
  190. epd1->exponent = exponent;
  191. EpdNormalize(epd1);
  192. }
  193. /**Function********************************************************************
  194. Synopsis [Multiplies two arbitrary precision double values.]
  195. Description [Multiplies two arbitrary precision double values.]
  196. SideEffects []
  197. SeeAlso []
  198. ******************************************************************************/
  199. void
  200. EpdMultiply2Decimal(EpDouble *epd1, EpDouble *epd2)
  201. {
  202. double value;
  203. int exponent;
  204. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  205. EpdMakeNan(epd1);
  206. return;
  207. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  208. int sign;
  209. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  210. EpdMakeInf(epd1, sign);
  211. return;
  212. }
  213. value = epd1->type.value * epd2->type.value;
  214. exponent = epd1->exponent + epd2->exponent;
  215. epd1->type.value = value;
  216. epd1->exponent = exponent;
  217. EpdNormalizeDecimal(epd1);
  218. }
  219. /**Function********************************************************************
  220. Synopsis [Multiplies two arbitrary precision double values.]
  221. Description [Multiplies two arbitrary precision double values.]
  222. SideEffects []
  223. SeeAlso []
  224. ******************************************************************************/
  225. void
  226. EpdMultiply3(EpDouble *epd1, EpDouble *epd2, EpDouble *epd3)
  227. {
  228. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  229. EpdMakeNan(epd1);
  230. return;
  231. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  232. int sign;
  233. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  234. EpdMakeInf(epd3, sign);
  235. return;
  236. }
  237. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  238. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  239. epd3->type.value = epd1->type.value * epd2->type.value;
  240. epd3->exponent = epd1->exponent + epd2->exponent;
  241. EpdNormalize(epd3);
  242. }
  243. /**Function********************************************************************
  244. Synopsis [Multiplies two arbitrary precision double values.]
  245. Description [Multiplies two arbitrary precision double values.]
  246. SideEffects []
  247. SeeAlso []
  248. ******************************************************************************/
  249. void
  250. EpdMultiply3Decimal(EpDouble *epd1, EpDouble *epd2, EpDouble *epd3)
  251. {
  252. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  253. EpdMakeNan(epd1);
  254. return;
  255. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  256. int sign;
  257. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  258. EpdMakeInf(epd3, sign);
  259. return;
  260. }
  261. epd3->type.value = epd1->type.value * epd2->type.value;
  262. epd3->exponent = epd1->exponent + epd2->exponent;
  263. EpdNormalizeDecimal(epd3);
  264. }
  265. /**Function********************************************************************
  266. Synopsis [Divides two arbitrary precision double values.]
  267. Description [Divides two arbitrary precision double values.]
  268. SideEffects []
  269. SeeAlso []
  270. ******************************************************************************/
  271. void
  272. EpdDivide(EpDouble *epd1, double value)
  273. {
  274. EpDouble epd2;
  275. double tmp;
  276. int exponent;
  277. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  278. EpdMakeNan(epd1);
  279. return;
  280. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  281. int sign;
  282. EpdConvert(value, &epd2);
  283. if (EpdIsInf(epd1) && IsInfDouble(value)) {
  284. EpdMakeNan(epd1);
  285. } else if (EpdIsInf(epd1)) {
  286. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  287. EpdMakeInf(epd1, sign);
  288. } else {
  289. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  290. EpdMakeZero(epd1, sign);
  291. }
  292. return;
  293. }
  294. if (value == 0.0) {
  295. EpdMakeNan(epd1);
  296. return;
  297. }
  298. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  299. EpdConvert(value, &epd2);
  300. tmp = epd1->type.value / epd2.type.value;
  301. exponent = epd1->exponent - epd2.exponent;
  302. epd1->type.value = tmp;
  303. epd1->exponent = exponent;
  304. EpdNormalize(epd1);
  305. }
  306. /**Function********************************************************************
  307. Synopsis [Divides two arbitrary precision double values.]
  308. Description [Divides two arbitrary precision double values.]
  309. SideEffects []
  310. SeeAlso []
  311. ******************************************************************************/
  312. void
  313. EpdDivide2(EpDouble *epd1, EpDouble *epd2)
  314. {
  315. double value;
  316. int exponent;
  317. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  318. EpdMakeNan(epd1);
  319. return;
  320. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  321. int sign;
  322. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  323. EpdMakeNan(epd1);
  324. } else if (EpdIsInf(epd1)) {
  325. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  326. EpdMakeInf(epd1, sign);
  327. } else {
  328. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  329. EpdMakeZero(epd1, sign);
  330. }
  331. return;
  332. }
  333. if (epd2->type.value == 0.0) {
  334. EpdMakeNan(epd1);
  335. return;
  336. }
  337. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  338. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  339. value = epd1->type.value / epd2->type.value;
  340. exponent = epd1->exponent - epd2->exponent;
  341. epd1->type.value = value;
  342. epd1->exponent = exponent;
  343. EpdNormalize(epd1);
  344. }
  345. /**Function********************************************************************
  346. Synopsis [Divides two arbitrary precision double values.]
  347. Description [Divides two arbitrary precision double values.]
  348. SideEffects []
  349. SeeAlso []
  350. ******************************************************************************/
  351. void
  352. EpdDivide3(EpDouble *epd1, EpDouble *epd2, EpDouble *epd3)
  353. {
  354. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  355. EpdMakeNan(epd3);
  356. return;
  357. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  358. int sign;
  359. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  360. EpdMakeNan(epd3);
  361. } else if (EpdIsInf(epd1)) {
  362. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  363. EpdMakeInf(epd3, sign);
  364. } else {
  365. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  366. EpdMakeZero(epd3, sign);
  367. }
  368. return;
  369. }
  370. if (epd2->type.value == 0.0) {
  371. EpdMakeNan(epd3);
  372. return;
  373. }
  374. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  375. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  376. epd3->type.value = epd1->type.value / epd2->type.value;
  377. epd3->exponent = epd1->exponent - epd2->exponent;
  378. EpdNormalize(epd3);
  379. }
  380. /**Function********************************************************************
  381. Synopsis [Adds two arbitrary precision double values.]
  382. Description [Adds two arbitrary precision double values.]
  383. SideEffects []
  384. SeeAlso []
  385. ******************************************************************************/
  386. void
  387. EpdAdd(EpDouble *epd1, double value)
  388. {
  389. EpDouble epd2;
  390. double tmp;
  391. int exponent, diff;
  392. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  393. EpdMakeNan(epd1);
  394. return;
  395. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  396. int sign;
  397. EpdConvert(value, &epd2);
  398. if (EpdIsInf(epd1) && IsInfDouble(value)) {
  399. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  400. if (sign == 1)
  401. EpdMakeNan(epd1);
  402. } else if (EpdIsInf(&epd2)) {
  403. EpdCopy(&epd2, epd1);
  404. }
  405. return;
  406. }
  407. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  408. EpdConvert(value, &epd2);
  409. if (epd1->exponent > epd2.exponent) {
  410. diff = epd1->exponent - epd2.exponent;
  411. if (diff <= EPD_MAX_BIN)
  412. tmp = epd1->type.value + epd2.type.value / pow((double)2.0, (double)diff);
  413. else
  414. tmp = epd1->type.value;
  415. exponent = epd1->exponent;
  416. } else if (epd1->exponent < epd2.exponent) {
  417. diff = epd2.exponent - epd1->exponent;
  418. if (diff <= EPD_MAX_BIN)
  419. tmp = epd1->type.value / pow((double)2.0, (double)diff) + epd2.type.value;
  420. else
  421. tmp = epd2.type.value;
  422. exponent = epd2.exponent;
  423. } else {
  424. tmp = epd1->type.value + epd2.type.value;
  425. exponent = epd1->exponent;
  426. }
  427. epd1->type.value = tmp;
  428. epd1->exponent = exponent;
  429. EpdNormalize(epd1);
  430. }
  431. /**Function********************************************************************
  432. Synopsis [Adds two arbitrary precision double values.]
  433. Description [Adds two arbitrary precision double values.]
  434. SideEffects []
  435. SeeAlso []
  436. ******************************************************************************/
  437. void
  438. EpdAdd2(EpDouble *epd1, EpDouble *epd2)
  439. {
  440. double value;
  441. int exponent, diff;
  442. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  443. EpdMakeNan(epd1);
  444. return;
  445. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  446. int sign;
  447. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  448. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  449. if (sign == 1)
  450. EpdMakeNan(epd1);
  451. } else if (EpdIsInf(epd2)) {
  452. EpdCopy(epd2, epd1);
  453. }
  454. return;
  455. }
  456. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  457. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  458. if (epd1->exponent > epd2->exponent) {
  459. diff = epd1->exponent - epd2->exponent;
  460. if (diff <= EPD_MAX_BIN) {
  461. value = epd1->type.value +
  462. epd2->type.value / pow((double)2.0, (double)diff);
  463. } else
  464. value = epd1->type.value;
  465. exponent = epd1->exponent;
  466. } else if (epd1->exponent < epd2->exponent) {
  467. diff = epd2->exponent - epd1->exponent;
  468. if (diff <= EPD_MAX_BIN) {
  469. value = epd1->type.value / pow((double)2.0, (double)diff) +
  470. epd2->type.value;
  471. } else
  472. value = epd2->type.value;
  473. exponent = epd2->exponent;
  474. } else {
  475. value = epd1->type.value + epd2->type.value;
  476. exponent = epd1->exponent;
  477. }
  478. epd1->type.value = value;
  479. epd1->exponent = exponent;
  480. EpdNormalize(epd1);
  481. }
  482. /**Function********************************************************************
  483. Synopsis [Adds two arbitrary precision double values.]
  484. Description [Adds two arbitrary precision double values.]
  485. SideEffects []
  486. SeeAlso []
  487. ******************************************************************************/
  488. void
  489. EpdAdd3(EpDouble *epd1, EpDouble *epd2, EpDouble *epd3)
  490. {
  491. double value;
  492. int exponent, diff;
  493. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  494. EpdMakeNan(epd3);
  495. return;
  496. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  497. int sign;
  498. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  499. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  500. if (sign == 1)
  501. EpdMakeNan(epd3);
  502. else
  503. EpdCopy(epd1, epd3);
  504. } else if (EpdIsInf(epd1)) {
  505. EpdCopy(epd1, epd3);
  506. } else {
  507. EpdCopy(epd2, epd3);
  508. }
  509. return;
  510. }
  511. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  512. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  513. if (epd1->exponent > epd2->exponent) {
  514. diff = epd1->exponent - epd2->exponent;
  515. if (diff <= EPD_MAX_BIN) {
  516. value = epd1->type.value +
  517. epd2->type.value / pow((double)2.0, (double)diff);
  518. } else
  519. value = epd1->type.value;
  520. exponent = epd1->exponent;
  521. } else if (epd1->exponent < epd2->exponent) {
  522. diff = epd2->exponent - epd1->exponent;
  523. if (diff <= EPD_MAX_BIN) {
  524. value = epd1->type.value / pow((double)2.0, (double)diff) +
  525. epd2->type.value;
  526. } else
  527. value = epd2->type.value;
  528. exponent = epd2->exponent;
  529. } else {
  530. value = epd1->type.value + epd2->type.value;
  531. exponent = epd1->exponent;
  532. }
  533. epd3->type.value = value;
  534. epd3->exponent = exponent;
  535. EpdNormalize(epd3);
  536. }
  537. /**Function********************************************************************
  538. Synopsis [Subtracts two arbitrary precision double values.]
  539. Description [Subtracts two arbitrary precision double values.]
  540. SideEffects []
  541. SeeAlso []
  542. ******************************************************************************/
  543. void
  544. EpdSubtract(EpDouble *epd1, double value)
  545. {
  546. EpDouble epd2;
  547. double tmp;
  548. int exponent, diff;
  549. if (EpdIsNan(epd1) || IsNanDouble(value)) {
  550. EpdMakeNan(epd1);
  551. return;
  552. } else if (EpdIsInf(epd1) || IsInfDouble(value)) {
  553. int sign;
  554. EpdConvert(value, &epd2);
  555. if (EpdIsInf(epd1) && IsInfDouble(value)) {
  556. sign = epd1->type.bits.sign ^ epd2.type.bits.sign;
  557. if (sign == 0)
  558. EpdMakeNan(epd1);
  559. } else if (EpdIsInf(&epd2)) {
  560. EpdCopy(&epd2, epd1);
  561. }
  562. return;
  563. }
  564. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  565. EpdConvert(value, &epd2);
  566. if (epd1->exponent > epd2.exponent) {
  567. diff = epd1->exponent - epd2.exponent;
  568. if (diff <= EPD_MAX_BIN)
  569. tmp = epd1->type.value - epd2.type.value / pow((double)2.0, (double)diff);
  570. else
  571. tmp = epd1->type.value;
  572. exponent = epd1->exponent;
  573. } else if (epd1->exponent < epd2.exponent) {
  574. diff = epd2.exponent - epd1->exponent;
  575. if (diff <= EPD_MAX_BIN)
  576. tmp = epd1->type.value / pow((double)2.0, (double)diff) - epd2.type.value;
  577. else
  578. tmp = epd2.type.value * (double)(-1.0);
  579. exponent = epd2.exponent;
  580. } else {
  581. tmp = epd1->type.value - epd2.type.value;
  582. exponent = epd1->exponent;
  583. }
  584. epd1->type.value = tmp;
  585. epd1->exponent = exponent;
  586. EpdNormalize(epd1);
  587. }
  588. /**Function********************************************************************
  589. Synopsis [Subtracts two arbitrary precision double values.]
  590. Description [Subtracts two arbitrary precision double values.]
  591. SideEffects []
  592. SeeAlso []
  593. ******************************************************************************/
  594. void
  595. EpdSubtract2(EpDouble *epd1, EpDouble *epd2)
  596. {
  597. double value;
  598. int exponent, diff;
  599. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  600. EpdMakeNan(epd1);
  601. return;
  602. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  603. int sign;
  604. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  605. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  606. if (sign == 0)
  607. EpdMakeNan(epd1);
  608. } else if (EpdIsInf(epd2)) {
  609. EpdCopy(epd2, epd1);
  610. }
  611. return;
  612. }
  613. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  614. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  615. if (epd1->exponent > epd2->exponent) {
  616. diff = epd1->exponent - epd2->exponent;
  617. if (diff <= EPD_MAX_BIN) {
  618. value = epd1->type.value -
  619. epd2->type.value / pow((double)2.0, (double)diff);
  620. } else
  621. value = epd1->type.value;
  622. exponent = epd1->exponent;
  623. } else if (epd1->exponent < epd2->exponent) {
  624. diff = epd2->exponent - epd1->exponent;
  625. if (diff <= EPD_MAX_BIN) {
  626. value = epd1->type.value / pow((double)2.0, (double)diff) -
  627. epd2->type.value;
  628. } else
  629. value = epd2->type.value * (double)(-1.0);
  630. exponent = epd2->exponent;
  631. } else {
  632. value = epd1->type.value - epd2->type.value;
  633. exponent = epd1->exponent;
  634. }
  635. epd1->type.value = value;
  636. epd1->exponent = exponent;
  637. EpdNormalize(epd1);
  638. }
  639. /**Function********************************************************************
  640. Synopsis [Subtracts two arbitrary precision double values.]
  641. Description [Subtracts two arbitrary precision double values.]
  642. SideEffects []
  643. SeeAlso []
  644. ******************************************************************************/
  645. void
  646. EpdSubtract3(EpDouble *epd1, EpDouble *epd2, EpDouble *epd3)
  647. {
  648. double value;
  649. int exponent, diff;
  650. if (EpdIsNan(epd1) || EpdIsNan(epd2)) {
  651. EpdMakeNan(epd3);
  652. return;
  653. } else if (EpdIsInf(epd1) || EpdIsInf(epd2)) {
  654. int sign;
  655. if (EpdIsInf(epd1) && EpdIsInf(epd2)) {
  656. sign = epd1->type.bits.sign ^ epd2->type.bits.sign;
  657. if (sign == 0)
  658. EpdCopy(epd1, epd3);
  659. else
  660. EpdMakeNan(epd3);
  661. } else if (EpdIsInf(epd1)) {
  662. EpdCopy(epd1, epd1);
  663. } else {
  664. sign = epd2->type.bits.sign ^ 0x1;
  665. EpdMakeInf(epd3, sign);
  666. }
  667. return;
  668. }
  669. assert(epd1->type.bits.exponent == EPD_MAX_BIN);
  670. assert(epd2->type.bits.exponent == EPD_MAX_BIN);
  671. if (epd1->exponent > epd2->exponent) {
  672. diff = epd1->exponent - epd2->exponent;
  673. if (diff <= EPD_MAX_BIN) {
  674. value = epd1->type.value -
  675. epd2->type.value / pow((double)2.0, (double)diff);
  676. } else
  677. value = epd1->type.value;
  678. exponent = epd1->exponent;
  679. } else if (epd1->exponent < epd2->exponent) {
  680. diff = epd2->exponent - epd1->exponent;
  681. if (diff <= EPD_MAX_BIN) {
  682. value = epd1->type.value / pow((double)2.0, (double)diff) -
  683. epd2->type.value;
  684. } else
  685. value = epd2->type.value * (double)(-1.0);
  686. exponent = epd2->exponent;
  687. } else {
  688. value = epd1->type.value - epd2->type.value;
  689. exponent = epd1->exponent;
  690. }
  691. epd3->type.value = value;
  692. epd3->exponent = exponent;
  693. EpdNormalize(epd3);
  694. }
  695. /**Function********************************************************************
  696. Synopsis [Computes arbitrary precision pow of base 2.]
  697. Description [Computes arbitrary precision pow of base 2.]
  698. SideEffects []
  699. SeeAlso []
  700. ******************************************************************************/
  701. void
  702. EpdPow2(int n, EpDouble *epd)
  703. {
  704. if (n <= EPD_MAX_BIN) {
  705. EpdConvert(pow((double)2.0, (double)n), epd);
  706. } else {
  707. EpDouble epd1, epd2;
  708. int n1, n2;
  709. n1 = n / 2;
  710. n2 = n - n1;
  711. EpdPow2(n1, &epd1);
  712. EpdPow2(n2, &epd2);
  713. EpdMultiply3(&epd1, &epd2, epd);
  714. }
  715. }
  716. /**Function********************************************************************
  717. Synopsis [Computes arbitrary precision pow of base 2.]
  718. Description [Computes arbitrary precision pow of base 2.]
  719. SideEffects []
  720. SeeAlso []
  721. ******************************************************************************/
  722. void
  723. EpdPow2Decimal(int n, EpDouble *epd)
  724. {
  725. if (n <= EPD_MAX_BIN) {
  726. epd->type.value = pow((double)2.0, (double)n);
  727. epd->exponent = 0;
  728. EpdNormalizeDecimal(epd);
  729. } else {
  730. EpDouble epd1, epd2;
  731. int n1, n2;
  732. n1 = n / 2;
  733. n2 = n - n1;
  734. EpdPow2Decimal(n1, &epd1);
  735. EpdPow2Decimal(n2, &epd2);
  736. EpdMultiply3Decimal(&epd1, &epd2, epd);
  737. }
  738. }
  739. /**Function********************************************************************
  740. Synopsis [Normalize an arbitrary precision double value.]
  741. Description [Normalize an arbitrary precision double value.]
  742. SideEffects []
  743. SeeAlso []
  744. ******************************************************************************/
  745. void
  746. EpdNormalize(EpDouble *epd)
  747. {
  748. int exponent;
  749. if (IsNanOrInfDouble(epd->type.value)) {
  750. epd->exponent = 0;
  751. return;
  752. }
  753. exponent = EpdGetExponent(epd->type.value);
  754. if (exponent == EPD_MAX_BIN)
  755. return;
  756. exponent -= EPD_MAX_BIN;
  757. epd->type.bits.exponent = EPD_MAX_BIN;
  758. epd->exponent += exponent;
  759. }
  760. /**Function********************************************************************
  761. Synopsis [Normalize an arbitrary precision double value.]
  762. Description [Normalize an arbitrary precision double value.]
  763. SideEffects []
  764. SeeAlso []
  765. ******************************************************************************/
  766. void
  767. EpdNormalizeDecimal(EpDouble *epd)
  768. {
  769. int exponent;
  770. if (IsNanOrInfDouble(epd->type.value)) {
  771. epd->exponent = 0;
  772. return;
  773. }
  774. exponent = EpdGetExponentDecimal(epd->type.value);
  775. epd->type.value /= pow((double)10.0, (double)exponent);
  776. epd->exponent += exponent;
  777. }
  778. /**Function********************************************************************
  779. Synopsis [Returns value and decimal exponent of EpDouble.]
  780. Description [Returns value and decimal exponent of EpDouble.]
  781. SideEffects []
  782. SeeAlso []
  783. ******************************************************************************/
  784. void
  785. EpdGetValueAndDecimalExponent(EpDouble *epd, double *value, int *exponent)
  786. {
  787. EpDouble epd1, epd2;
  788. if (EpdIsNanOrInf(epd))
  789. return;
  790. if (EpdIsZero(epd)) {
  791. *value = 0.0;
  792. *exponent = 0;
  793. return;
  794. }
  795. epd1.type.value = epd->type.value;
  796. epd1.exponent = 0;
  797. EpdPow2Decimal(epd->exponent, &epd2);
  798. EpdMultiply2Decimal(&epd1, &epd2);
  799. *value = epd1.type.value;
  800. *exponent = epd1.exponent;
  801. }
  802. /**Function********************************************************************
  803. Synopsis [Returns the exponent value of a double.]
  804. Description [Returns the exponent value of a double.]
  805. SideEffects []
  806. SeeAlso []
  807. ******************************************************************************/
  808. int
  809. EpdGetExponent(double value)
  810. {
  811. int exponent;
  812. EpDouble epd;
  813. epd.type.value = value;
  814. exponent = epd.type.bits.exponent;
  815. return(exponent);
  816. }
  817. /**Function********************************************************************
  818. Synopsis [Returns the decimal exponent value of a double.]
  819. Description [Returns the decimal exponent value of a double.]
  820. SideEffects []
  821. SeeAlso []
  822. ******************************************************************************/
  823. int
  824. EpdGetExponentDecimal(double value)
  825. {
  826. char *pos, str[24];
  827. int exponent;
  828. sprintf(str, "%E", value);
  829. pos = strstr(str, "E");
  830. sscanf(pos, "E%d", &exponent);
  831. return(exponent);
  832. }
  833. /**Function********************************************************************
  834. Synopsis [Makes EpDouble Inf.]
  835. Description [Makes EpDouble Inf.]
  836. SideEffects []
  837. SeeAlso []
  838. ******************************************************************************/
  839. void
  840. EpdMakeInf(EpDouble *epd, int sign)
  841. {
  842. epd->type.bits.mantissa1 = 0;
  843. epd->type.bits.mantissa0 = 0;
  844. epd->type.bits.exponent = EPD_EXP_INF;
  845. epd->type.bits.sign = sign;
  846. epd->exponent = 0;
  847. }
  848. /**Function********************************************************************
  849. Synopsis [Makes EpDouble Zero.]
  850. Description [Makes EpDouble Zero.]
  851. SideEffects []
  852. SeeAlso []
  853. ******************************************************************************/
  854. void
  855. EpdMakeZero(EpDouble *epd, int sign)
  856. {
  857. epd->type.bits.mantissa1 = 0;
  858. epd->type.bits.mantissa0 = 0;
  859. epd->type.bits.exponent = 0;
  860. epd->type.bits.sign = sign;
  861. epd->exponent = 0;
  862. }
  863. /**Function********************************************************************
  864. Synopsis [Makes EpDouble NaN.]
  865. Description [Makes EpDouble NaN.]
  866. SideEffects []
  867. SeeAlso []
  868. ******************************************************************************/
  869. void
  870. EpdMakeNan(EpDouble *epd)
  871. {
  872. epd->type.nan.mantissa1 = 0;
  873. epd->type.nan.mantissa0 = 0;
  874. epd->type.nan.quiet_bit = 1;
  875. epd->type.nan.exponent = EPD_EXP_INF;
  876. epd->type.nan.sign = 1;
  877. epd->exponent = 0;
  878. }
  879. /**Function********************************************************************
  880. Synopsis [Copies a EpDouble struct.]
  881. Description [Copies a EpDouble struct.]
  882. SideEffects []
  883. SeeAlso []
  884. ******************************************************************************/
  885. void
  886. EpdCopy(EpDouble *from, EpDouble *to)
  887. {
  888. to->type.value = from->type.value;
  889. to->exponent = from->exponent;
  890. }
  891. /**Function********************************************************************
  892. Synopsis [Checks whether the value is Inf.]
  893. Description [Checks whether the value is Inf.]
  894. SideEffects []
  895. SeeAlso []
  896. ******************************************************************************/
  897. int
  898. EpdIsInf(EpDouble *epd)
  899. {
  900. return(IsInfDouble(epd->type.value));
  901. }
  902. /**Function********************************************************************
  903. Synopsis [Checks whether the value is Zero.]
  904. Description [Checks whether the value is Zero.]
  905. SideEffects []
  906. SeeAlso []
  907. ******************************************************************************/
  908. int
  909. EpdIsZero(EpDouble *epd)
  910. {
  911. if (epd->type.value == 0.0)
  912. return(1);
  913. else
  914. return(0);
  915. }
  916. /**Function********************************************************************
  917. Synopsis [Checks whether the value is NaN.]
  918. Description [Checks whether the value is NaN.]
  919. SideEffects []
  920. SeeAlso []
  921. ******************************************************************************/
  922. int
  923. EpdIsNan(EpDouble *epd)
  924. {
  925. return(IsNanDouble(epd->type.value));
  926. }
  927. /**Function********************************************************************
  928. Synopsis [Checks whether the value is NaN or Inf.]
  929. Description [Checks whether the value is NaN or Inf.]
  930. SideEffects []
  931. SeeAlso []
  932. ******************************************************************************/
  933. int
  934. EpdIsNanOrInf(EpDouble *epd)
  935. {
  936. return(IsNanOrInfDouble(epd->type.value));
  937. }
  938. /**Function********************************************************************
  939. Synopsis [Checks whether the value is Inf.]
  940. Description [Checks whether the value is Inf.]
  941. SideEffects []
  942. SeeAlso []
  943. ******************************************************************************/
  944. int
  945. IsInfDouble(double value)
  946. {
  947. EpType val;
  948. val.value = value;
  949. if (val.bits.exponent == EPD_EXP_INF &&
  950. val.bits.mantissa0 == 0 &&
  951. val.bits.mantissa1 == 0) {
  952. if (val.bits.sign == 0)
  953. return(1);
  954. else
  955. return(-1);
  956. }
  957. return(0);
  958. }
  959. /**Function********************************************************************
  960. Synopsis [Checks whether the value is NaN.]
  961. Description [Checks whether the value is NaN.]
  962. SideEffects []
  963. SeeAlso []
  964. ******************************************************************************/
  965. int
  966. IsNanDouble(double value)
  967. {
  968. EpType val;
  969. val.value = value;
  970. if (val.nan.exponent == EPD_EXP_INF &&
  971. val.nan.sign == 1 &&
  972. val.nan.quiet_bit == 1 &&
  973. val.nan.mantissa0 == 0 &&
  974. val.nan.mantissa1 == 0) {
  975. return(1);
  976. }
  977. return(0);
  978. }
  979. /**Function********************************************************************
  980. Synopsis [Checks whether the value is NaN or Inf.]
  981. Description [Checks whether the value is NaN or Inf.]
  982. SideEffects []
  983. SeeAlso []
  984. ******************************************************************************/
  985. int
  986. IsNanOrInfDouble(double value)
  987. {
  988. EpType val;
  989. val.value = value;
  990. if (val.nan.exponent == EPD_EXP_INF &&
  991. val.nan.mantissa0 == 0 &&
  992. val.nan.mantissa1 == 0 &&
  993. (val.nan.sign == 1 || val.nan.quiet_bit == 0)) {
  994. return(1);
  995. }
  996. return(0);
  997. }