test-math.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/TestSuite.h>
  27. #include <math.h>
  28. TEST_CASE(trig)
  29. {
  30. EXPECT_CLOSE(sin(1234), 0.601927);
  31. EXPECT_CLOSE(cos(1234), -0.798550);
  32. EXPECT_CLOSE(tan(1234), -0.753775);
  33. EXPECT_CLOSE(sqrt(1234), 35.128336);
  34. EXPECT_CLOSE(sin(-1), -0.8414709848078965);
  35. EXPECT_CLOSE(cos(-1), 0.5403023058681398);
  36. EXPECT_CLOSE(tan(-1), -1.5574077246549023);
  37. EXPECT(isnan(sqrt(-1)));
  38. EXPECT(isnan(asin(1.1)));
  39. EXPECT(isnan(asin(-1.1)));
  40. EXPECT_CLOSE(asin(0), 0.0);
  41. EXPECT_CLOSE(asin(0.01), 0.01);
  42. EXPECT_CLOSE(asin(0.1), 0.100167);
  43. EXPECT_CLOSE(asin(0.3), 0.304693);
  44. EXPECT_CLOSE(asin(0.499), 0.522444);
  45. EXPECT_CLOSE(asin(0.5), 0.523599);
  46. EXPECT_CLOSE(asin(0.501), 0.524754);
  47. EXPECT_CLOSE(asin(0.9), 1.119770);
  48. EXPECT_CLOSE(asin(0.99), 1.429245);
  49. EXPECT_CLOSE(asin(1.0), 1.570750);
  50. EXPECT_CLOSE(atan(0), 0.0);
  51. EXPECT_CLOSE(atan(0.5), 0.463648);
  52. EXPECT_CLOSE(atan(-0.5), -0.463648);
  53. EXPECT_CLOSE(atan(5.5), 1.390943);
  54. EXPECT_CLOSE(atan(-5.5), -1.390943);
  55. EXPECT_CLOSE(atan(555.5), 1.568996);
  56. }
  57. TEST_CASE(other)
  58. {
  59. EXPECT_EQ(trunc(9999999999999.5), 9999999999999.0);
  60. EXPECT_EQ(trunc(-9999999999999.5), -9999999999999.0);
  61. }
  62. TEST_CASE(exponents)
  63. {
  64. struct values {
  65. double x;
  66. double exp;
  67. double sinh;
  68. double cosh;
  69. double tanh;
  70. };
  71. values values[8] {
  72. { 1.500000, 4.481626, 2.129246, 2.352379, 0.905148 },
  73. { 20.990000, 1304956710.432035, 652478355.216017, 652478355.216017, 1.000000 },
  74. { 20.010000, 490041186.687082, 245020593.343541, 245020593.343541, 1.000000 },
  75. { 0.000000, 1.000000, 0.000000, 1.000000, 0.000000 },
  76. { 0.010000, 1.010050, 0.010000, 1.000050, 0.010000 },
  77. { -0.010000, 0.990050, -0.010000, 1.000050, -0.010000 },
  78. { -1.000000, 0.367879, -1.175201, 1.543081, -0.761594 },
  79. { -17.000000, 0.000000, -12077476.376788, 12077476.376788, -1.000000 },
  80. };
  81. for (auto& v : values) {
  82. EXPECT_CLOSE(exp(v.x), v.exp);
  83. EXPECT_CLOSE(sinh(v.x), v.sinh);
  84. EXPECT_CLOSE(cosh(v.x), v.cosh);
  85. EXPECT_CLOSE(tanh(v.x), v.tanh);
  86. }
  87. EXPECT_EQ(exp(1000), __builtin_huge_val());
  88. }
  89. TEST_CASE(logarithms)
  90. {
  91. EXPECT(isnan(log(-1)));
  92. EXPECT(log(0) < -1000000);
  93. EXPECT_CLOSE(log(0.5), -0.693233);
  94. EXPECT_CLOSE(log(1.1), 0.095310);
  95. EXPECT_CLOSE(log(5), 1.609480);
  96. EXPECT_CLOSE(log(5.5), 1.704842);
  97. EXPECT_CLOSE(log(500), 6.214104);
  98. EXPECT_CLOSE(log2(5), 2.321989);
  99. EXPECT_CLOSE(log10(5), 0.698988);
  100. }
  101. union Extractor {
  102. explicit Extractor(double d)
  103. : d(d)
  104. {
  105. }
  106. Extractor(unsigned sign, unsigned exponent, unsigned long long mantissa)
  107. : mantissa(mantissa)
  108. , exponent(exponent)
  109. , sign(sign)
  110. {
  111. }
  112. struct {
  113. unsigned long long mantissa : 52;
  114. unsigned exponent : 11;
  115. unsigned sign : 1;
  116. };
  117. double d;
  118. bool operator==(const Extractor& other) const
  119. {
  120. return other.sign == sign && other.exponent == exponent && other.mantissa == mantissa;
  121. }
  122. };
  123. namespace AK {
  124. template<>
  125. struct Formatter<Extractor> : StandardFormatter {
  126. void format(FormatBuilder& builder, const Extractor& value)
  127. {
  128. builder.put_literal("{");
  129. builder.put_u64(value.sign);
  130. builder.put_literal(", ");
  131. builder.put_u64(value.exponent, 16, true);
  132. builder.put_literal(", ");
  133. builder.put_u64(value.mantissa, 16, true);
  134. builder.put_literal("}");
  135. }
  136. };
  137. }
  138. static Extractor nextafter_translator(Extractor x, Extractor target)
  139. {
  140. return Extractor(nextafter(x.d, target.d));
  141. }
  142. TEST_CASE(nextafter)
  143. {
  144. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x7fe, 0xfffffffffffff), Extractor(0x0, 0x7fe, 0xfffffffffffff)), Extractor(0x0, 0x7fe, 0xfffffffffffff));
  145. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x1, 0x0), Extractor(0x0, 0x412, 0xe848000000000)), Extractor(0x0, 0x1, 0x1));
  146. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x3ff, 0x0), Extractor(0x0, 0x412, 0xe848200000000)), Extractor(0x0, 0x3ff, 0x1));
  147. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x0, 0x0), Extractor(0x0, 0x412, 0xe848000000000)), Extractor(0x0, 0x0, 0x1));
  148. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x0), Extractor(0x0, 0x412, 0xe848000000000)), Extractor(0x0, 0x0, 0x1));
  149. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x3ff, 0x0), Extractor(0x0, 0x412, 0xe847e00000000)), Extractor(0x1, 0x3fe, 0xfffffffffffff));
  150. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x1), Extractor(0x0, 0x412, 0xe848000000000)), Extractor(0x0, 0x0, 0x2));
  151. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x7fe, 0xfffffffffffff), Extractor(0x0, 0x7fe, 0xfffffffffffff)), Extractor(0x0, 0x7fe, 0xfffffffffffff));
  152. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x412, 0xe848000000000), Extractor(0x0, 0x1, 0x0)), Extractor(0x0, 0x412, 0xe847fffffffff));
  153. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x412, 0xe848200000000), Extractor(0x0, 0x3ff, 0x0)), Extractor(0x0, 0x412, 0xe8481ffffffff));
  154. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x412, 0xe848000000000), Extractor(0x1, 0x0, 0x0)), Extractor(0x0, 0x412, 0xe847fffffffff));
  155. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x412, 0xe848000000000), Extractor(0x0, 0x0, 0x0)), Extractor(0x0, 0x412, 0xe847fffffffff));
  156. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x412, 0xe847e00000000), Extractor(0x1, 0x3ff, 0x0)), Extractor(0x0, 0x412, 0xe847dffffffff));
  157. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x412, 0xe848000000000), Extractor(0x0, 0x0, 0x1)), Extractor(0x0, 0x412, 0xe847fffffffff));
  158. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x7fe, 0xfffffffffffff), Extractor(0x0, 0x7fe, 0xfffffffffffff)), Extractor(0x0, 0x7fe, 0xfffffffffffff));
  159. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x1, 0x0), Extractor(0x0, 0x1, 0x0)), Extractor(0x0, 0x1, 0x0));
  160. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x3ff, 0x0), Extractor(0x0, 0x3ff, 0x0)), Extractor(0x0, 0x3ff, 0x0));
  161. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x0, 0x0), Extractor(0x1, 0x0, 0x0)), Extractor(0x1, 0x0, 0x0));
  162. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x0), Extractor(0x0, 0x0, 0x0)), Extractor(0x0, 0x0, 0x0));
  163. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x3ff, 0x0), Extractor(0x1, 0x3ff, 0x0)), Extractor(0x1, 0x3ff, 0x0));
  164. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x1), Extractor(0x0, 0x0, 0x1)), Extractor(0x0, 0x0, 0x1));
  165. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x7fe, 0xfffffffffffff), Extractor(0x0, 0x7fe, 0xfffffffffffff)), Extractor(0x1, 0x7fe, 0xffffffffffffe));
  166. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x1, 0x0), Extractor(0x0, 0x1, 0x0)), Extractor(0x1, 0x0, 0xfffffffffffff));
  167. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x3ff, 0x0), Extractor(0x0, 0x3ff, 0x0)), Extractor(0x1, 0x3fe, 0xfffffffffffff));
  168. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x0), Extractor(0x1, 0x0, 0x0)), Extractor(0x1, 0x0, 0x0));
  169. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x0, 0x0), Extractor(0x0, 0x0, 0x0)), Extractor(0x0, 0x0, 0x0));
  170. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x3ff, 0x0), Extractor(0x1, 0x3ff, 0x0)), Extractor(0x0, 0x3fe, 0xfffffffffffff));
  171. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x0, 0x1), Extractor(0x0, 0x0, 0x1)), Extractor(0x1, 0x0, 0x0));
  172. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x7fe, 0xfffffffffffff), Extractor(0x1, 0x7fe, 0xfffffffffffff)), Extractor(0x0, 0x7fe, 0xffffffffffffe));
  173. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x1, 0x0), Extractor(0x1, 0x1, 0x0)), Extractor(0x0, 0x0, 0xfffffffffffff));
  174. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x3ff, 0x0), Extractor(0x1, 0x3ff, 0x0)), Extractor(0x0, 0x3fe, 0xfffffffffffff));
  175. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x0, 0x0), Extractor(0x0, 0x0, 0x0)), Extractor(0x0, 0x0, 0x0));
  176. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x0), Extractor(0x1, 0x0, 0x0)), Extractor(0x1, 0x0, 0x0));
  177. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x3ff, 0x0), Extractor(0x0, 0x3ff, 0x0)), Extractor(0x1, 0x3fe, 0xfffffffffffff));
  178. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x1), Extractor(0x1, 0x0, 0x1)), Extractor(0x0, 0x0, 0x0));
  179. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x7fe, 0xfffffffffffff), Extractor(0x0, 0x7fe, 0xfffffffffffff)), Extractor(0x0, 0x7fe, 0xfffffffffffff));
  180. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x1, 0x0), Extractor(0x1, 0x419, 0x7d78400000000)), Extractor(0x0, 0x0, 0xfffffffffffff));
  181. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x3ff, 0x0), Extractor(0x1, 0x419, 0x7d783fc000000)), Extractor(0x0, 0x3fe, 0xfffffffffffff));
  182. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x0, 0x0), Extractor(0x1, 0x419, 0x7d78400000000)), Extractor(0x1, 0x0, 0x1));
  183. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x0), Extractor(0x1, 0x419, 0x7d78400000000)), Extractor(0x1, 0x0, 0x1));
  184. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x3ff, 0x0), Extractor(0x1, 0x419, 0x7d78404000000)), Extractor(0x1, 0x3ff, 0x1));
  185. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x0, 0x1), Extractor(0x1, 0x419, 0x7d78400000000)), Extractor(0x0, 0x0, 0x0));
  186. EXPECT_EQ(nextafter_translator(Extractor(0x0, 0x7fe, 0xfffffffffffff), Extractor(0x0, 0x7fe, 0xfffffffffffff)), Extractor(0x0, 0x7fe, 0xfffffffffffff));
  187. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x419, 0x7d78400000000), Extractor(0x0, 0x1, 0x0)), Extractor(0x1, 0x419, 0x7d783ffffffff));
  188. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x419, 0x7d783fc000000), Extractor(0x0, 0x3ff, 0x0)), Extractor(0x1, 0x419, 0x7d783fbffffff));
  189. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x419, 0x7d78400000000), Extractor(0x1, 0x0, 0x0)), Extractor(0x1, 0x419, 0x7d783ffffffff));
  190. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x419, 0x7d78400000000), Extractor(0x0, 0x0, 0x0)), Extractor(0x1, 0x419, 0x7d783ffffffff));
  191. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x419, 0x7d78404000000), Extractor(0x1, 0x3ff, 0x0)), Extractor(0x1, 0x419, 0x7d78403ffffff));
  192. EXPECT_EQ(nextafter_translator(Extractor(0x1, 0x419, 0x7d78400000000), Extractor(0x0, 0x0, 0x1)), Extractor(0x1, 0x419, 0x7d783ffffffff));
  193. }
  194. TEST_MAIN(Math)