File: numeric_type.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (110 lines) | stat: -rw-r--r-- 2,635 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
{xrst_begin numeric_type.cpp}

The NumericType: Example and Test
#################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end numeric_type.cpp}
*/
// BEGIN C++

# include <cppad/cppad.hpp>

namespace { // Empty namespace

   // -------------------------------------------------------------------
   class MyType {
   private:
      double d;
   public:
      // constructor from void
      MyType(void) : d(0.)
      { }
      // constructor from an int
      MyType(int d_) : d(d_)
      { }
      // copy constructor
      MyType(const MyType &x)
      {  d = x.d; }
      // assignment operator
      void operator = (const MyType &x)
      {  d = x.d; }
      // member function that converts to double
      double Double(void) const
      {  return d; }
      // unary plus
      MyType operator + (void) const
      {  MyType x;
         x.d =  d;
         return x;
      }
      // unary plus
      MyType operator - (void) const
      {  MyType x;
         x.d = - d;
         return x;
      }
      // binary addition
      MyType operator + (const MyType &x) const
      {  MyType y;
         y.d = d + x.d ;
         return y;
      }
      // binary subtraction
      MyType operator - (const MyType &x) const
      {  MyType y;
         y.d = d - x.d ;
         return y;
      }
      // binary multiplication
      MyType operator * (const MyType &x) const
      {  MyType y;
         y.d = d * x.d ;
         return y;
      }
      // binary division
      MyType operator / (const MyType &x) const
      {  MyType y;
         y.d = d / x.d ;
         return y;
      }
      // compound assignment addition
      void operator += (const MyType &x)
      {  d += x.d; }
      // compound assignment subtraction
      void operator -= (const MyType &x)
      {  d -= x.d; }
      // compound assignment multiplication
      void operator *= (const MyType &x)
      {  d *= x.d; }
      // compound assignment division
      void operator /= (const MyType &x)
      {  d /= x.d; }
   };
}
bool NumericType(void)
{  bool ok  = true;
   using CppAD::AD;
   using CppAD::CheckNumericType;

   CheckNumericType<MyType>            ();

   CheckNumericType<int>               ();
   CheckNumericType<double>            ();
   CheckNumericType< AD<double> >      ();
   CheckNumericType< AD< AD<double> > >();

   return ok;
}

// END C++