KiCad PCB EDA Suite
SPICE_VALUE Class Reference

Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)

More...

#include <spice_value.h>

Public Types

enum  UNIT_PREFIX {
  PFX_FEMTO = -15, PFX_PICO = -12, PFX_NANO = -9, PFX_MICRO = -6,
  PFX_MILI = -3, PFX_NONE = 0, PFX_KILO = 3, PFX_MEGA = 6,
  PFX_GIGA = 9, PFX_TERA = 12
}
 

Public Member Functions

 SPICE_VALUE ()
 
 SPICE_VALUE (const wxString &aString)
 

Parses the string to create a Spice value (e.g. 100n)

More...
 
 SPICE_VALUE (int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
 
 SPICE_VALUE (double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
 
void Normalize ()
 Normalizes the value. More...
 
double ToDouble () const
 
wxString ToString () const
 Returns string value as when converting double to string (e.g. More...
 
wxString ToSpiceString () const
 Returns string value in Spice format (e.g. More...
 
wxString ToOrigString () const
 Returns either a normal string or Spice format string, depending on the original value format. More...
 
bool IsSpiceString () const
 Returns true if the object was initiated with a Spice formatted string value. More...
 
bool operator== (const SPICE_VALUE &aOther) const
 
bool operator> (const SPICE_VALUE &aOther) const
 
bool operator< (const SPICE_VALUE &aOther) const
 
bool operator>= (const SPICE_VALUE &aOther) const
 
bool operator<= (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator- (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator+ (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator* (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator/ (const SPICE_VALUE &aOther) const
 

Static Private Member Functions

static void stripZeros (wxString &aString)
 

Removes redundant zeros from the end of a string.

More...
 

Private Attributes

double m_base
 
UNIT_PREFIX m_prefix
 
bool m_spiceStr
 

Was the value defined using the Spice notation?

More...
 

Detailed Description

Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)

Definition at line 32 of file spice_value.h.

Member Enumeration Documentation

Enumerator
PFX_FEMTO 
PFX_PICO 
PFX_NANO 
PFX_MICRO 
PFX_MILI 
PFX_NONE 
PFX_KILO 
PFX_MEGA 
PFX_GIGA 
PFX_TERA 

Definition at line 35 of file spice_value.h.

Constructor & Destructor Documentation

SPICE_VALUE::SPICE_VALUE ( )
inline

Definition at line 49 of file spice_value.h.

50  : m_base( 0 ), m_prefix( PFX_NONE ), m_spiceStr( false )
51  {
52  }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE::SPICE_VALUE ( const wxString &  aString)

Parses the string to create a Spice value (e.g. 100n)

Definition at line 35 of file spice_value.cpp.

References dummy(), m_base, m_prefix, m_spiceStr, Normalize(), PFX_FEMTO, PFX_GIGA, PFX_KILO, PFX_MEGA, PFX_MICRO, PFX_MILI, PFX_NANO, PFX_NONE, PFX_PICO, and PFX_TERA.

36 {
37  char buf[8] = { 0, };
38 
39  if( aString.IsEmpty() )
40  throw std::invalid_argument( "Spice value cannot be empty" );
41 
42  LOCALE_IO dummy; // All numeric values should be in "C" locale(decimal separator = .)
43 
44  if( sscanf( (const char*) aString.c_str(), "%lf%7s", &m_base, buf ) == 0 )
45  throw std::invalid_argument( "Invalid Spice value string" );
46 
47  if( *buf == 0 )
48  {
50  m_spiceStr = false;
51  Normalize();
52  return;
53  }
54 
55  m_spiceStr = true;
56 
57  for( char* bufPtr = buf; *bufPtr; ++bufPtr )
58  *bufPtr = tolower( *bufPtr );
59 
60  if( !strcmp( buf, "meg" ) )
61  {
63  }
64  else
65  {
66  switch( buf[0] )
67  {
68  case 'f': m_prefix = PFX_FEMTO; break;
69  case 'p': m_prefix = PFX_PICO; break;
70  case 'n': m_prefix = PFX_NANO; break;
71  case 'u': m_prefix = PFX_MICRO; break;
72  case 'm': m_prefix = PFX_MILI; break;
73  case 'k': m_prefix = PFX_KILO; break;
74  case 'g': m_prefix = PFX_GIGA; break;
75  case 't': m_prefix = PFX_TERA; break;
76 
77  default:
78  throw std::invalid_argument( "Invalid unit prefix" );
79  }
80  }
81 
82  Normalize();
83 }
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:86
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
SPICE_VALUE::SPICE_VALUE ( int  aInt,
UNIT_PREFIX  aPrefix = PFX_NONE 
)
inline

Definition at line 57 of file spice_value.h.

References Normalize().

58  : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
59  {
60  Normalize();
61  }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:86
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE::SPICE_VALUE ( double  aDouble,
UNIT_PREFIX  aPrefix = PFX_NONE 
)
inline

Definition at line 63 of file spice_value.h.

References Normalize().

64  : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
65  {
66  Normalize();
67  }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:86
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138

Member Function Documentation

bool SPICE_VALUE::IsSpiceString ( ) const
inline

Returns true if the object was initiated with a Spice formatted string value.

Definition at line 98 of file spice_value.h.

References m_spiceStr.

Referenced by TUNER_SLIDER::updateValueText().

99  {
100  return m_spiceStr;
101  }
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
void SPICE_VALUE::Normalize ( )

Normalizes the value.

The unit prefix is picked so the base is (0.001 <= base < 1000).

Definition at line 86 of file spice_value.cpp.

References m_base, and m_prefix.

Referenced by operator*(), operator+(), operator-(), operator/(), and SPICE_VALUE().

87 {
88  while( std::fabs( m_base ) >= 1000.0 )
89  {
90  m_base *= 0.001;
91  m_prefix = (UNIT_PREFIX)( m_prefix + 3 );
92  }
93 
94  while( m_base != 0.0 && std::fabs( m_base ) < 1.000 )
95  {
96  m_base *= 1000.0;
97  m_prefix = (UNIT_PREFIX)( m_prefix - 3 );
98  }
99 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
SPICE_VALUE SPICE_VALUE::operator* ( const SPICE_VALUE aOther) const

Definition at line 206 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

207 {
208  SPICE_VALUE res( m_base * aOther.m_base, (UNIT_PREFIX)( m_prefix + aOther.m_prefix ) );
209  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
210  res.Normalize();
211 
212  return res;
213 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE SPICE_VALUE::operator+ ( const SPICE_VALUE aOther) const

Definition at line 144 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

145 {
146  int prefixDiff = m_prefix - aOther.m_prefix;
147  SPICE_VALUE res;
148  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
149 
150  // Convert both numbers to a common prefix
151  if( prefixDiff > 0 )
152  {
153  // Switch to the aOther prefix
154  res.m_base = ( m_base * std::pow( 10, prefixDiff ) ) + aOther.m_base;
155  res.m_prefix = aOther.m_prefix;
156  }
157  else if( prefixDiff < 0 )
158  {
159  // Use the current prefix
160  res.m_base = m_base + ( aOther.m_base * std::pow( 10, -prefixDiff ) );
161  res.m_prefix = m_prefix;
162  }
163  else
164  {
165  res.m_base = m_base + aOther.m_base;
166  res.m_prefix = m_prefix; // == aOther.m_prefix
167  }
168 
169  res.Normalize();
170 
171  return res;
172 }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:86
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE SPICE_VALUE::operator- ( const SPICE_VALUE aOther) const

Definition at line 175 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

176 {
177  int prefixDiff = m_prefix - aOther.m_prefix;
178  SPICE_VALUE res;
179  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
180 
181  // Convert both numbers to a common prefix
182  if( prefixDiff > 0 )
183  {
184  // Switch to the aOther prefix
185  res.m_base = m_base * std::pow( 10, prefixDiff ) - aOther.m_base;
186  res.m_prefix = aOther.m_prefix;
187  }
188  else if( prefixDiff < 0 )
189  {
190  // Use the current prefix
191  res.m_base = m_base - aOther.m_base * std::pow( 10, -prefixDiff );
192  res.m_prefix = m_prefix;
193  }
194  else
195  {
196  res.m_base = m_base - aOther.m_base;
197  res.m_prefix = m_prefix; // == aOther.m_prefix
198  }
199 
200  res.Normalize();
201 
202  return res;
203 }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:86
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE SPICE_VALUE::operator/ ( const SPICE_VALUE aOther) const

Definition at line 216 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

217 {
218  SPICE_VALUE res( m_base / aOther.m_base, (UNIT_PREFIX)( m_prefix - aOther.m_prefix ) );
219  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
220  res.Normalize();
221 
222  return res;
223 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
bool SPICE_VALUE::operator< ( const SPICE_VALUE aOther) const
inline

Definition at line 113 of file spice_value.h.

References ToDouble().

114  {
115  return this->ToDouble() < aOther.ToDouble();
116  }
double ToDouble() const
bool SPICE_VALUE::operator<= ( const SPICE_VALUE aOther) const
inline

Definition at line 123 of file spice_value.h.

124  {
125  return ( *this == aOther || *this < aOther );
126  }
bool SPICE_VALUE::operator== ( const SPICE_VALUE aOther) const
inline

Definition at line 103 of file spice_value.h.

References m_base, and m_prefix.

104  {
105  return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
106  }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool SPICE_VALUE::operator> ( const SPICE_VALUE aOther) const
inline

Definition at line 108 of file spice_value.h.

References ToDouble().

109  {
110  return this->ToDouble() > aOther.ToDouble();
111  }
double ToDouble() const
bool SPICE_VALUE::operator>= ( const SPICE_VALUE aOther) const
inline

Definition at line 118 of file spice_value.h.

119  {
120  return ( *this == aOther || *this > aOther );
121  }
void SPICE_VALUE::stripZeros ( wxString &  aString)
staticprivate

Removes redundant zeros from the end of a string.

Definition at line 226 of file spice_value.cpp.

Referenced by ToSpiceString(), and ToString().

227 {
228  if ( aString.Find( ',' ) >= 0 || aString.Find( '.' ) >= 0 )
229  {
230  while( aString.EndsWith( '0' ) )
231  aString.RemoveLast();
232 
233  if( aString.EndsWith( '.' ) || aString.EndsWith( ',' ) )
234  aString.RemoveLast();
235  }
236 }
double SPICE_VALUE::ToDouble ( ) const

Definition at line 102 of file spice_value.cpp.

References m_base, m_prefix, and PFX_NONE.

Referenced by operator<(), operator>(), and ToString().

103 {
104  double res = m_base;
105 
106  if( m_prefix != PFX_NONE )
107  res *= std::pow( 10, (int) m_prefix );
108 
109  return res;
110 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
wxString SPICE_VALUE::ToOrigString ( ) const
inline

Returns either a normal string or Spice format string, depending on the original value format.

Definition at line 90 of file spice_value.h.

References m_spiceStr, ToSpiceString(), and ToString().

Referenced by TUNER_SLIDER::onMaxTextEnter(), TUNER_SLIDER::onMinTextEnter(), TUNER_SLIDER::onSave(), TUNER_SLIDER::onValueTextEnter(), TUNER_SLIDER::SetMax(), TUNER_SLIDER::SetMin(), and TUNER_SLIDER::TUNER_SLIDER().

91  {
92  return m_spiceStr ? ToSpiceString() : ToString();
93  }
wxString ToSpiceString() const
Returns string value in Spice format (e.g.
wxString ToString() const
Returns string value as when converting double to string (e.g.
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
wxString SPICE_VALUE::ToSpiceString ( ) const

Returns string value in Spice format (e.g.

123.3456789k).

Definition at line 121 of file spice_value.cpp.

References m_base, m_prefix, PFX_FEMTO, PFX_GIGA, PFX_KILO, PFX_MEGA, PFX_MICRO, PFX_MILI, PFX_NANO, PFX_NONE, PFX_PICO, PFX_TERA, and stripZeros().

Referenced by SIM_PLOT_FRAME::onCursorUpdate(), DIALOG_SPICE_MODEL::parsePowerSource(), ToOrigString(), and TUNER_SLIDER::updateValueText().

122 {
123  wxString res = wxString::FromCDouble( m_base );
124  stripZeros( res );
125 
126  switch( m_prefix )
127  {
128  case PFX_FEMTO: res += "f"; break;
129  case PFX_PICO: res += "p"; break;
130  case PFX_NANO: res += "n"; break;
131  case PFX_MICRO: res += "u"; break;
132  case PFX_MILI: res += "m"; break;
133  case PFX_NONE: break;
134  case PFX_KILO: res += "k"; break;
135  case PFX_MEGA: res += "Meg"; break;
136  case PFX_GIGA: res += "G"; break;
137  case PFX_TERA: res += "T"; break;
138  }
139 
140  return res;
141 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
static void stripZeros(wxString &aString)
Removes redundant zeros from the end of a string.
wxString SPICE_VALUE::ToString ( ) const

Returns string value as when converting double to string (e.g.

123456.789).

Definition at line 112 of file spice_value.cpp.

References Format(), stripZeros(), and ToDouble().

Referenced by ToOrigString(), and TUNER_SLIDER::updateValueText().

113 {
114  wxString res( wxString::Format( "%.3f", ToDouble() ) );
115  stripZeros( res );
116 
117  return res;
118 }
static void stripZeros(wxString &aString)
Removes redundant zeros from the end of a string.
double ToDouble() const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205

Member Data Documentation

double SPICE_VALUE::m_base
private
UNIT_PREFIX SPICE_VALUE::m_prefix
private
bool SPICE_VALUE::m_spiceStr
private

Was the value defined using the Spice notation?

Definition at line 138 of file spice_value.h.

Referenced by IsSpiceString(), operator*(), operator+(), operator-(), operator/(), SPICE_VALUE(), and ToOrigString().


The documentation for this class was generated from the following files: