~vbursian/research-assistant/intervers

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
////////////////////////////////////////////////////////////////////////////////
/*! @file PhysValue.h   Физвеличина.
- Part of RANet - Research Assistant Net Library (based on ANSI C++).
- Copyright(C) 2010, Viktor E. Bursian, St.Petersburg, Russia.
                     Viktor.Bursian@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#ifndef PhysValue_H
#define PhysValue_H
#include "BasicMath.h"
#include "Units.h"
namespace RA {
//------------------------------------------------------------------------------

ANNOUNCE_CLASS(sPhysValue)
ANNOUNCE_CLASS(xRAlgebra)

//--------------------------------------------------------------- sPhysValue ---
/*! Физвеличина - вещественное число с погрешностью и единицами измерения.


@todo{Fucking_Qt!} Q_DECLARE_METATYPE not in a right place
                  (requered for my templates AddTransition,..)

*/
class RANet_EXPORT  sPhysValue : public virtual sMathValue
                                 /*! @todo{PhysValues} virtual?*/
{
  STORABLE(sPhysValue)

  public://static
    static const unsigned short int MaxDigits = 7;
    static unsigned short int DraftNormalization; //always
    static unsigned short int BriefNormalization; //for Text()
    static unsigned short int SmartNormalization; //for editing

  public:
                              sPhysValue ();
    explicit                  sPhysValue (rcsUnits  units);
                              sPhysValue (real value ,rcsUnits  units);
                              sPhysValue (real value ,real error
                                         ,rcsUnits units);
    explicit                  sPhysValue (real value);
                              sPhysValue (real value ,real error);
    explicit                  sPhysValue (sString  UnitsStr);
                              sPhysValue (real value ,sString  UnitsStr);
                              sPhysValue (real value ,real error
                                         ,sString  UnitsStr);
                              sPhysValue (rcsPhysValue  R);
    virtual psMathValue       Replica () const
                                { return new sPhysValue(*this); }

  public:
    bool                      UnitsAreFixed () const
                                { return UnitsAreFixedFlag; }
    void                      FixTheUnits ()
                                { UnitsAreFixedFlag = true; }
    sUnits                    Units () const
                                { return TheUnits; }
    sPhysValue                Error () const
                                {
                                  return sPhysValue(TheErrorMantissa
                                                            *power10(TheOrder)
                                                   ,TheUnits );
                                }
    rsPhysValue               SetError (rcsPhysValue  error);
    rsPhysValue               SetRelativeError (real  error);
    bool                      IsInf () const
                                { return isinf(TheValueMantissa); }
    bool                      IsNaN () const
                                { return isnan(TheValueMantissa); }
    bool                      IsPowerOfTen () const
                                { return ThePowerOfTenFlag; }
    bool                      IsUnitsless () const;
                                //!< Tests for being in fact unitless
    real                      ToReal () const;
                                //!< Converts a unitless expression to real
                                //note: a type conversion operator to real
                                //would bring ambiguosity to math operators
//    sString                   Text (eTextFormat        format=Plain
//                                   ,eTextDetalization  detalization=Casual);
//                                //!< Visual representation for user
//    sString                   Text (eTextFormat        format=Plain
//                                   ,eTextDetalization  detalization=Casual)
//                                   const;
//                                //!< Visual representation for user
    virtual sString           Text (eTextFormat        format=Plain
                                   ,eTextDetalization  detalization=Casual);
                                //!< Visual representation for user
    virtual sString           Text (eTextFormat        format=Plain
                                   ,eTextDetalization  detalization=Casual)
                                   const;
                                //!< Visual representation for user
    void                      Normalize ()
                                { Normalize(SmartNormalization); }

  public://operators
    rsPhysValue               operator = (rcsPhysValue);
//    bool                      operator == (rcsPhysValue) const;
    bool                      operator < (rcsPhysValue) const;
    sPhysValue                operator + () const;
    sPhysValue                operator - () const;
    sPhysValue                operator + (rcsPhysValue) const;
    sPhysValue                operator - (rcsPhysValue) const;
    sPhysValue                operator * (rcsPhysValue) const;
    sPhysValue                operator / (rcsPhysValue) const;
    sPhysValue                operator * (real  r) const
                                { return (*this) * sPhysValue(r); }
    sPhysValue                operator / (real  r) const
                                { return (*this) / sPhysValue(r); }
    rsPhysValue               operator += (rcsPhysValue);
    rsPhysValue               operator -= (rcsPhysValue);
    rsPhysValue               operator *= (real);
    rsPhysValue               operator /= (real);
    rsPhysValue               operator << (int  order_shift);


  public://but for internal needs in RANet and RAGUI
    real                      ValueMantissa () const
                                { return TheValueMantissa; }
    real                      ErrorMantissa () const
                                { return TheErrorMantissa; }
    int                       Order () const
                                { return TheOrder; }
    unsigned short int        Digits () const
                                { return TheDigits; }
                                //!< число цифр после запятой при выводе,
                                //!  актуальное с учётом погрешности

  private:
    void                      Normalize (unsigned short int  level);
    sString                   Txt (eTextFormat        format=Plain
                                  ,eTextDetalization  detalization=Casual)
                                  const;

  private: //fields
    sUnits                    TheUnits;
    bool                      UnitsAreFixedFlag;
    int                       TheOrder;
    real                      TheValueMantissa;
    real                      TheErrorMantissa;
    bool                      PreciseFlag;
    bool                      ThePowerOfTenFlag;
    unsigned short int        TheDigits;
    unsigned short int        NormalizationLevel;
                                  //!< 0 - possibly not normalized;
                                  //!< 1 - units - untouched,
                                  //!<     mantissa&order - OK;
                                  //!< 2 - units - within permissions;
                                  //!< n - units are looking better,
                                  //!<     time consuming limited by c*exp(n)

  friend class sUnits;
  friend class sPhysRange;
};


inline  sPhysValue  operator * (real  r ,sPhysValue  v)
{
  return sPhysValue(r) * v;
}


inline  sPhysValue  operator / (real  r ,sPhysValue  v)
{
  return sPhysValue(r) / v;
}


inline  sPhysValue  Abs (rcsPhysValue  V)
{
  return ( V.ValueMantissa() < 0.0 ? -V : V );
}

//---------------------------------------------------------------- xRAlgebra ---

class RANet_EXPORT  xRAlgebra : public xException
{
  public:
                              xRAlgebra
                                  (rcsString  msg_text
                                  ,rcsString  src_file_name = sString()
                                  ,int        line_no       = 0
                                  ,rcsString  class_name
                                                       = sString("xRAlgebra")
                                  )
                                  :xException(msg_text,src_file_name,line_no
                                             ,class_name)
                                {}
//                              xRAlgebra (literal  FName
//                                        ,int      LineNo);
//                              xRAlgebra (literal  FName
//                                        ,int      LineNo
//                                        ,literal  What);
};

//------------------------------------------------------------------------------
} //namespace RA


#include <QMetaType>
Q_DECLARE_METATYPE(RA::sPhysValue)

#endif