Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp:
- #define USEGMP
- #include "./bu.h"
- #include "bn.h"
- #include "mpf_class2.h"
- #include<iostream>
- typedef const char* cstr;
- int main() {
- vect_t v = VINIT_ZERO;
- bu_log("hello vector: %lf, %lf, %lf\n", V3ARGS(v));
- //std::cout << "hello vector: " << (long)v[X] << ", " << (long) v[Y] << ", " << (long)v[Z] << std::endl;
- fastf_t a(1.999);
- std::cout << "1.999/13.51566=" << (a/fastf_t(13.51566)) << std::endl;
- std::cout << "1.999 + 0.023=" << (a + fastf_t(0.023)) << std::endl;
- std::cout << "1.999 - 0.0235=" << ( a - fastf_t(0.0235)) << std::endl;
- std::cout << "1.999* 13.51566=" << (a * fastf_t(13.51566)) << std::endl;
- std::cout << "1.999² = " <<( a * a )<< std::endl;
- fastf_t b(a);
- b += 3.55556;
- std::cout << "b = " << (b) << "; a = " << (a) << std::endl;
- std::cout << "double b = " << (double) b << std::endl;
- //vect_t v1 = {1.5,1.2,-1}, v2 = {-9,6,3.25};
- point_t v1 = {1.2,1.5,-1}, v2={-9,-3,-2};
- //bu_log("Distance between v1 and v2: %6.6f\n", bn_dist_pt3_pt3(v1, v2));
- std::cout << "Distance betewen v1 and v2: " << bn_dist_pt3_pt3(v1, v2) << std::endl;
- return 0;
- }
- mpf_class2.h:
- #pragma once
- extern "C++" {
- #include <gmpxx.h>
- }
- #include<string>
- /*
- Extension class for mpf_class2
- by: Quincy Lam
- mpf_class already implements:
- - operator=
- */
- class mpf_class2
- /* : public mpf_class */
- {
- /*
- Wraps mpf_t in operator overloads
- Written by Quincy Lam
- Defining a copy constructor shall break bu_log
- */
- public:
- mpf_class2(double d);
- mpf_class2(signed long sl);
- mpf_class2(signed si);
- mpf_class2(const mpf_t& f0);
- /*
- mpf_class2(const mpf_class2& c);
- */
- mpf_class2& operator+=(const mpf_class2& rhs);
- mpf_class2& operator+=(unsigned long rhs);
- mpf_class2& operator+=(double rhs);
- const mpf_class2 operator+(const mpf_class2& rhs) const;
- mpf_class2& operator-=(const mpf_class2& rhs);
- mpf_class2& operator-=(unsigned long rhs);
- mpf_class2& operator-=(double rhs);
- const mpf_class2 operator-(const mpf_class2& rhs) const;
- mpf_class2& operator*=(const mpf_class2& rhs);
- mpf_class2& operator*=(unsigned long rhs);
- mpf_class2& operator*=(double rhs);
- const mpf_class2 operator*(const mpf_class2& rhs) const;
- mpf_class2& operator/=(const mpf_class2& rhs);
- mpf_class2& operator/=(unsigned long rhs);
- mpf_class2& operator/=(double rhs);
- const mpf_class2 operator/(const mpf_class2& rhs) const;
- mpf_class2& operator=(const mpf_class2& rhs);
- mpf_class2& operator=(signed long rhs);
- mpf_class2& operator=(unsigned long rhs);
- mpf_class2& operator=(double rhs);
- bool operator==(const mpf_class2& rhs) const;
- bool operator!=(const mpf_class2& rhs) const;
- bool operator<(const mpf_class2& rhs) const;
- bool operator>(const mpf_class2& rhs) const;
- bool operator<=(const mpf_class2& rhs) const;
- bool operator>=(const mpf_class2& rhs) const;
- const mpf_class2 operator-() const;
- operator double() const;
- operator std::string() const;
- /*
- operator signed long() const;
- */
- const mpf_t& get_f() const {
- return f;
- }
- static const long precision;
- private:
- mpf_t f;
- };
- mpf_class2.cpp:
- #include "mpf_class2.h"
- const long mpf_class2::precision = 8*500;
- mpf_class2& mpf_class2::operator+=(const mpf_class2& rhs)
- {
- mpf_add(f, f, rhs.get_f());
- return *this;
- }
- mpf_class2& mpf_class2::operator+=(unsigned long rhs)
- {
- mpf_add_ui(f, f, rhs);
- return *this;
- }
- mpf_class2& mpf_class2::operator+=(double rhs)
- {
- operator+=(mpf_class2(rhs));
- return *this;
- }
- const mpf_class2 mpf_class2::operator+(const mpf_class2& rhs) const
- {
- mpf_class2 r(f);
- r += rhs;
- return r;
- }
- mpf_class2& mpf_class2::operator-=(const mpf_class2& rhs)
- {
- mpf_sub(f, f, rhs.get_f());
- return *this;
- }
- mpf_class2& mpf_class2::operator-=(unsigned long rhs)
- {
- mpf_sub_ui(f, f, rhs);
- return *this;
- }
- mpf_class2& mpf_class2::operator-=(double rhs)
- {
- operator-=(mpf_class2(rhs));
- return *this;
- }
- const mpf_class2 mpf_class2::operator-(const mpf_class2& rhs) const
- {
- mpf_class2 r(f);
- r -= rhs;
- return r;
- }
- mpf_class2& mpf_class2::operator*=(const mpf_class2& rhs)
- {
- mpf_mul(f, f, rhs.get_f());
- return *this;
- }
- mpf_class2& mpf_class2::operator*=(unsigned long rhs)
- {
- mpf_mul_ui(f, f, rhs);
- return *this;
- }
- mpf_class2& mpf_class2::operator*=(double rhs)
- {
- operator*=(mpf_class2(rhs));
- return *this;
- }
- const mpf_class2 mpf_class2::operator*(const mpf_class2& rhs) const
- {
- mpf_class2 r(f);
- r *= rhs;
- return r;
- }
- mpf_class2& mpf_class2::operator/=(const mpf_class2& rhs)
- {
- mpf_div(f, f, rhs.get_f());
- return *this;
- }
- mpf_class2& mpf_class2::operator/=(unsigned long rhs)
- {
- mpf_div_ui(f, f, rhs);
- return *this;
- }
- mpf_class2& mpf_class2::operator/=(double rhs)
- {
- operator/=(mpf_class2(rhs));
- return *this;
- }
- const mpf_class2 mpf_class2::operator/(const mpf_class2& rhs) const
- {
- mpf_class2 r(f);
- r /= rhs;
- return r;
- }
- mpf_class2& mpf_class2::operator=(const mpf_class2& rhs)
- {
- mpf_set(f, rhs.get_f());
- return *this;
- }
- mpf_class2& mpf_class2::operator=(signed long rhs)
- {
- mpf_set_si(f,rhs);
- return *this;
- }
- mpf_class2& mpf_class2::operator=(unsigned long rhs)
- {
- mpf_set_ui(f,rhs);
- return *this;
- }
- mpf_class2& mpf_class2::operator=(double rhs)
- {
- mpf_set_d(f,rhs);
- return *this;
- }
- bool mpf_class2::operator==(const mpf_class2& rhs) const
- {
- return mpf_cmp(f, rhs.get_f()) == 0;
- }
- bool mpf_class2::operator!=(const mpf_class2& rhs) const
- {
- return !(*this == rhs);
- }
- bool mpf_class2::operator<(const mpf_class2& rhs) const
- {
- return mpf_cmp(f, rhs.get_f()) < 0;
- }
- bool mpf_class2::operator>(const mpf_class2& rhs) const
- {
- return mpf_cmp(f, rhs.get_f()) > 0;
- }
- bool mpf_class2::operator<=(const mpf_class2& rhs) const
- {
- return mpf_cmp(f, rhs.get_f()) <= 0;
- }
- bool mpf_class2::operator>=(const mpf_class2& rhs) const
- {
- return mpf_cmp(f, rhs.get_f()) >= 0;
- }
- const mpf_class2 mpf_class2::operator-() const
- {
- mpf_class2 r = *this;
- r *= (double)(-1);
- return r;
- }
- mpf_class2::operator double() const
- {
- return mpf_get_d(f);
- }
- mpf_class2::operator std::string() const
- {
- char* r = new char[50];
- mp_exp_t e;
- /*
- Store 50 digits of f in base 10 and store the index of the decimal place
- in e
- */
- mpf_get_str(r, &e, 10, 50, f);
- std::string s(r);
- /*
- Insert one decimal point at position e
- */
- s.insert(e, 1, '.');
- return s;
- }
- /*
- mpf_class2::operator signed long() const {
- return get_si();
- }
- */
- mpf_class2::mpf_class2(double d)
- {
- mpf_set_default_prec(precision);
- mpf_init_set_d(f, d);
- printf("double constructor with operand %f\n", d);
- }
- mpf_class2::mpf_class2(signed long sl)
- {
- mpf_set_default_prec(precision);
- mpf_init_set_si(f, sl);
- printf("sl constructor with operand %i\n", sl);
- }
- mpf_class2::mpf_class2(signed si)
- {
- mpf_set_default_prec(precision);
- mpf_init_set_si(f, si);
- printf("si constructor with operand %i\n", si);
- }
- mpf_class2::mpf_class2(const mpf_t& f0)
- {
- mpf_set_default_prec(precision);
- mpf_init_set(f, f0);
- printf("mpf constructor with operand %i\n", mpf_get_d(f0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement