Advertisement
Guest User

Untitled

a guest
Nov 29th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.37 KB | None | 0 0
  1. main.cpp:
  2.  
  3.  
  4. #define USEGMP
  5. #include "./bu.h"
  6. #include "bn.h"
  7.  
  8. #include "mpf_class2.h"
  9. #include<iostream>
  10.  
  11. typedef const char* cstr;
  12.  
  13. int main() {
  14.     vect_t v = VINIT_ZERO;
  15.  
  16.  
  17.     bu_log("hello vector: %lf, %lf, %lf\n", V3ARGS(v));
  18.     //std::cout << "hello vector: " << (long)v[X] <<  ", " << (long) v[Y] << ", " << (long)v[Z] << std::endl;
  19.  
  20.     fastf_t a(1.999);
  21.     std::cout << "1.999/13.51566=" << (a/fastf_t(13.51566)) << std::endl;
  22.     std::cout << "1.999 + 0.023=" << (a + fastf_t(0.023)) << std::endl;
  23.     std::cout << "1.999 - 0.0235=" << ( a - fastf_t(0.0235)) << std::endl;
  24.     std::cout << "1.999* 13.51566=" << (a * fastf_t(13.51566)) << std::endl;
  25.     std::cout << "1.999² = " <<( a * a )<< std::endl;
  26.     fastf_t b(a);
  27.     b += 3.55556;
  28.     std::cout << "b = " << (b) << "; a = " << (a) << std::endl;
  29.     std::cout << "double b = " << (double) b << std::endl;
  30.  
  31.  
  32.     //vect_t v1  = {1.5,1.2,-1}, v2 = {-9,6,3.25};
  33.     point_t v1 = {1.2,1.5,-1}, v2={-9,-3,-2};
  34.     //bu_log("Distance between v1 and v2: %6.6f\n", bn_dist_pt3_pt3(v1, v2));
  35.     std::cout << "Distance betewen v1 and v2: " << bn_dist_pt3_pt3(v1, v2) << std::endl;
  36.  
  37.     return 0;
  38. }
  39.  
  40.  
  41. mpf_class2.h:
  42.  
  43.  
  44. #pragma once
  45.  
  46. extern "C++" {
  47. #include <gmpxx.h>
  48. }
  49. #include<string>
  50. /*
  51. Extension class for mpf_class2
  52. by: Quincy Lam
  53. mpf_class already implements:
  54. - operator=
  55. */
  56.  
  57. class mpf_class2
  58. /* : public mpf_class */
  59. {
  60.     /*
  61.     Wraps mpf_t in operator overloads
  62.     Written by Quincy Lam
  63.     Defining a copy constructor shall break bu_log
  64.     */
  65. public:
  66.     mpf_class2(double d);
  67.     mpf_class2(signed long sl);
  68.     mpf_class2(signed si);
  69.     mpf_class2(const mpf_t& f0);
  70.     /*
  71.     mpf_class2(const mpf_class2& c);
  72.     */
  73.     mpf_class2& operator+=(const mpf_class2& rhs);
  74.     mpf_class2& operator+=(unsigned long rhs);
  75.     mpf_class2& operator+=(double rhs);
  76.     const mpf_class2 operator+(const mpf_class2& rhs) const;
  77.     mpf_class2& operator-=(const mpf_class2& rhs);
  78.     mpf_class2& operator-=(unsigned long rhs);
  79.     mpf_class2& operator-=(double rhs);
  80.     const mpf_class2 operator-(const mpf_class2& rhs) const;
  81.     mpf_class2& operator*=(const mpf_class2& rhs);
  82.     mpf_class2& operator*=(unsigned long rhs);
  83.     mpf_class2& operator*=(double rhs);
  84.     const mpf_class2 operator*(const mpf_class2& rhs) const;
  85.     mpf_class2& operator/=(const mpf_class2& rhs);
  86.     mpf_class2& operator/=(unsigned long rhs);
  87.     mpf_class2& operator/=(double rhs);
  88.     const mpf_class2 operator/(const mpf_class2& rhs) const;
  89.     mpf_class2& operator=(const mpf_class2& rhs);
  90.     mpf_class2& operator=(signed long rhs);
  91.     mpf_class2& operator=(unsigned long rhs);
  92.     mpf_class2& operator=(double rhs);
  93.     bool operator==(const mpf_class2& rhs) const;
  94.     bool operator!=(const mpf_class2& rhs) const;
  95.     bool operator<(const mpf_class2& rhs) const;
  96.     bool operator>(const mpf_class2& rhs) const;
  97.     bool operator<=(const mpf_class2& rhs) const;
  98.     bool operator>=(const mpf_class2& rhs) const;
  99.     const mpf_class2 operator-() const;
  100.  
  101.     operator double() const;
  102.     operator std::string() const;
  103.     /*
  104.     operator signed long() const;
  105.     */
  106.  
  107.     const mpf_t& get_f() const {
  108.         return f;
  109.     }
  110.  
  111.     static const long precision;
  112.  
  113.     private:
  114.     mpf_t f;
  115. };
  116.  
  117. mpf_class2.cpp:
  118.  
  119. #include "mpf_class2.h"
  120. const long mpf_class2::precision = 8*500;
  121.  
  122. mpf_class2& mpf_class2::operator+=(const mpf_class2& rhs)
  123. {
  124.     mpf_add(f, f, rhs.get_f());
  125.     return *this;
  126. }
  127.  
  128. mpf_class2& mpf_class2::operator+=(unsigned long rhs)
  129. {
  130.     mpf_add_ui(f, f, rhs);
  131.     return *this;
  132. }
  133.  
  134. mpf_class2& mpf_class2::operator+=(double rhs)
  135. {
  136.     operator+=(mpf_class2(rhs));
  137.     return *this;
  138. }
  139.  
  140. const mpf_class2 mpf_class2::operator+(const mpf_class2& rhs) const
  141. {
  142.     mpf_class2 r(f);
  143.     r += rhs;
  144.     return r;
  145. }
  146.  
  147. mpf_class2& mpf_class2::operator-=(const mpf_class2& rhs)
  148. {
  149.     mpf_sub(f, f, rhs.get_f());
  150.     return *this;
  151. }
  152.  
  153. mpf_class2& mpf_class2::operator-=(unsigned long rhs)
  154. {
  155.     mpf_sub_ui(f, f, rhs);
  156.     return *this;
  157. }
  158.  
  159. mpf_class2& mpf_class2::operator-=(double rhs)
  160. {
  161.     operator-=(mpf_class2(rhs));
  162.     return *this;
  163. }
  164.  
  165. const mpf_class2 mpf_class2::operator-(const mpf_class2& rhs) const
  166. {
  167.     mpf_class2 r(f);
  168.     r -= rhs;
  169.     return r;
  170. }
  171.  
  172.  
  173. mpf_class2& mpf_class2::operator*=(const mpf_class2& rhs)
  174. {
  175.     mpf_mul(f, f, rhs.get_f());
  176.     return *this;
  177. }
  178.  
  179. mpf_class2& mpf_class2::operator*=(unsigned long rhs)
  180. {
  181.     mpf_mul_ui(f, f, rhs);
  182.     return *this;
  183. }
  184.  
  185. mpf_class2& mpf_class2::operator*=(double rhs)
  186. {
  187.     operator*=(mpf_class2(rhs));
  188.     return *this;
  189. }
  190.  
  191. const mpf_class2 mpf_class2::operator*(const mpf_class2& rhs) const
  192. {
  193.     mpf_class2 r(f);
  194.     r *= rhs;
  195.     return r;
  196. }
  197.  
  198. mpf_class2& mpf_class2::operator/=(const mpf_class2& rhs)
  199. {
  200.     mpf_div(f, f, rhs.get_f());
  201.     return *this;
  202. }
  203.  
  204. mpf_class2& mpf_class2::operator/=(unsigned long rhs)
  205. {
  206.     mpf_div_ui(f, f, rhs);
  207.     return *this;
  208. }
  209.  
  210. mpf_class2& mpf_class2::operator/=(double rhs)
  211. {
  212.     operator/=(mpf_class2(rhs));
  213.     return *this;
  214. }
  215.  
  216. const mpf_class2 mpf_class2::operator/(const mpf_class2& rhs) const
  217. {
  218.     mpf_class2 r(f);
  219.     r /= rhs;
  220.     return r;
  221. }
  222.  
  223. mpf_class2& mpf_class2::operator=(const mpf_class2& rhs)
  224. {
  225.     mpf_set(f, rhs.get_f());
  226.     return *this;
  227. }
  228.  
  229. mpf_class2& mpf_class2::operator=(signed long rhs)
  230. {
  231.     mpf_set_si(f,rhs);
  232.     return *this;
  233. }
  234.  
  235. mpf_class2& mpf_class2::operator=(unsigned long rhs)
  236. {
  237.     mpf_set_ui(f,rhs);
  238.     return *this;
  239. }
  240.  
  241. mpf_class2& mpf_class2::operator=(double rhs)
  242. {
  243.     mpf_set_d(f,rhs);
  244.     return *this;
  245. }
  246.  
  247. bool mpf_class2::operator==(const mpf_class2& rhs) const
  248. {
  249.     return mpf_cmp(f, rhs.get_f()) == 0;
  250. }
  251.  
  252. bool mpf_class2::operator!=(const mpf_class2& rhs) const
  253. {
  254.     return !(*this == rhs);
  255. }
  256.  
  257. bool mpf_class2::operator<(const mpf_class2& rhs) const
  258. {
  259.     return mpf_cmp(f, rhs.get_f()) < 0;
  260. }
  261.  
  262. bool mpf_class2::operator>(const mpf_class2& rhs) const
  263. {
  264.     return mpf_cmp(f, rhs.get_f()) > 0;
  265. }
  266.  
  267. bool mpf_class2::operator<=(const mpf_class2& rhs) const
  268. {
  269.     return mpf_cmp(f, rhs.get_f()) <= 0;
  270. }
  271.  
  272. bool mpf_class2::operator>=(const mpf_class2& rhs) const
  273. {
  274.     return mpf_cmp(f, rhs.get_f()) >= 0;
  275. }
  276.  
  277. const mpf_class2 mpf_class2::operator-() const
  278. {
  279.     mpf_class2 r = *this;
  280.     r *= (double)(-1);
  281.     return r;
  282. }
  283.  
  284. mpf_class2::operator double() const
  285. {
  286.     return mpf_get_d(f);
  287. }
  288.  
  289. mpf_class2::operator std::string() const
  290. {
  291.     char* r = new char[50];
  292.     mp_exp_t e;
  293.     /*
  294.     Store 50 digits of f in base 10 and store the index of the decimal place
  295.     in e
  296.     */
  297.     mpf_get_str(r, &e, 10, 50, f);
  298.  
  299.     std::string s(r);
  300.     /*
  301.     Insert one decimal point at position e
  302.     */
  303.     s.insert(e, 1, '.');
  304.     return s;
  305. }
  306. /*
  307. mpf_class2::operator signed long() const {
  308.     return get_si();
  309. }
  310. */
  311. mpf_class2::mpf_class2(double d)
  312. {
  313.     mpf_set_default_prec(precision);
  314.     mpf_init_set_d(f, d);
  315.     printf("double constructor with operand %f\n", d);
  316. }
  317.  
  318. mpf_class2::mpf_class2(signed long sl)
  319. {
  320.     mpf_set_default_prec(precision);
  321.     mpf_init_set_si(f, sl);
  322.     printf("sl constructor with operand %i\n", sl);
  323. }
  324.  
  325. mpf_class2::mpf_class2(signed si)
  326. {
  327.     mpf_set_default_prec(precision);
  328.     mpf_init_set_si(f, si);
  329.     printf("si constructor with operand %i\n", si);
  330. }
  331.  
  332. mpf_class2::mpf_class2(const mpf_t& f0)
  333. {
  334.     mpf_set_default_prec(precision);
  335.     mpf_init_set(f, f0);
  336.     printf("mpf constructor with operand %i\n", mpf_get_d(f0));
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement