KiCad PCB EDA Suite
SPICE_VALUE Class Reference

< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spice formatted values 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 ()
 Parses the string to create a Spice value (e.g. 100n) More...
 
 SPICE_VALUE (const wxString &aString)
 
 SPICE_VALUE (int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
 
 SPICE_VALUE (double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
 
void Normalize ()
 Normalize the value. More...
 
double ToDouble () const
 
wxString ToString () const
 Return string value as when converting double to string (e.g. More...
 
wxString ToSpiceString () const
 Return string value in Spice format (e.g. More...
 
wxString ToOrigString () const
 Return either a normal string or Spice format string, depending on the original value format. More...
 
bool IsSpiceString () const
 Return 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)
 < Remove redundant zeros from the end of a string. More...
 

Private Attributes

double m_base
 
UNIT_PREFIX m_prefix
 Was the value defined using the Spice notation? More...
 
bool m_spiceStr
 

Detailed Description

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

Definition at line 34 of file spice_value.h.

Member Enumeration Documentation

◆ UNIT_PREFIX

Enumerator
PFX_FEMTO 
PFX_PICO 
PFX_NANO 
PFX_MICRO 
PFX_MILI 
PFX_NONE 
PFX_KILO 
PFX_MEGA 
PFX_GIGA 
PFX_TERA 

Definition at line 37 of file spice_value.h.

Constructor & Destructor Documentation

◆ SPICE_VALUE() [1/4]

SPICE_VALUE::SPICE_VALUE ( )
inline

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

Definition at line 51 of file spice_value.h.

◆ SPICE_VALUE() [2/4]

SPICE_VALUE::SPICE_VALUE ( const wxString &  aString)

Definition at line 37 of file spice_value.cpp.

38 {
39  char buf[8] = { 0, };
40 
41  if( aString.IsEmpty() )
42  throw KI_PARAM_ERROR( _( "Spice value cannot be empty" ) );
43 
44  LOCALE_IO dummy; // All numeric values should be in "C" locale(decimal separator = .)
45 
46  if( sscanf( (const char*) aString.c_str(), "%lf%7s", &m_base, buf ) == 0 )
47  throw KI_PARAM_ERROR( _( "Invalid Spice value string" ) );
48 
49  if( *buf == 0 )
50  {
52  m_spiceStr = false;
53  Normalize();
54  return;
55  }
56 
57  m_spiceStr = true;
58 
59  for( char* bufPtr = buf; *bufPtr; ++bufPtr )
60  *bufPtr = tolower( *bufPtr );
61 
62  if( !strcmp( buf, "meg" ) )
63  {
65  }
66  else
67  {
68  switch( buf[0] )
69  {
70  case 'f': m_prefix = PFX_FEMTO; break;
71  case 'p': m_prefix = PFX_PICO; break;
72  case 'n': m_prefix = PFX_NANO; break;
73  case 'u': m_prefix = PFX_MICRO; break;
74  case 'm': m_prefix = PFX_MILI; break;
75  case 'k': m_prefix = PFX_KILO; break;
76  case 'g': m_prefix = PFX_GIGA; break;
77  case 't': m_prefix = PFX_TERA; break;
78 
79  default:
80  throw KI_PARAM_ERROR( _( "Invalid unit prefix" ) );
81  }
82  }
83 
84  Normalize();
85 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
double m_base
Definition: spice_value.h:139
void Normalize()
Normalize the value.
Definition: spice_value.cpp:88
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
bool m_spiceStr
Definition: spice_value.h:143
#define _(s)
Definition: 3d_actions.cpp:33
Hold a translatable error message and may be used when throwing exceptions containing a translated er...
Definition: ki_exception.h:44

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.

◆ SPICE_VALUE() [3/4]

SPICE_VALUE::SPICE_VALUE ( int  aInt,
UNIT_PREFIX  aPrefix = PFX_NONE 
)
inline

Definition at line 59 of file spice_value.h.

60  : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
61  {
62  Normalize();
63  }
double m_base
Definition: spice_value.h:139
void Normalize()
Normalize the value.
Definition: spice_value.cpp:88
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
bool m_spiceStr
Definition: spice_value.h:143

References Normalize().

◆ SPICE_VALUE() [4/4]

SPICE_VALUE::SPICE_VALUE ( double  aDouble,
UNIT_PREFIX  aPrefix = PFX_NONE 
)
inline

Definition at line 65 of file spice_value.h.

66  : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
67  {
68  Normalize();
69  }
double m_base
Definition: spice_value.h:139
void Normalize()
Normalize the value.
Definition: spice_value.cpp:88
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
bool m_spiceStr
Definition: spice_value.h:143

References Normalize().

Member Function Documentation

◆ IsSpiceString()

bool SPICE_VALUE::IsSpiceString ( ) const
inline

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

Definition at line 100 of file spice_value.h.

101  {
102  return m_spiceStr;
103  }
bool m_spiceStr
Definition: spice_value.h:143

References m_spiceStr.

Referenced by TUNER_SLIDER::updateValueText().

◆ Normalize()

void SPICE_VALUE::Normalize ( )

Normalize the value.

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

Definition at line 88 of file spice_value.cpp.

89 {
90  while( std::fabs( m_base ) >= 1000.0 )
91  {
92  m_base *= 0.001;
93  m_prefix = (UNIT_PREFIX)( m_prefix + 3 );
94 
95  if( m_prefix == PFX_TERA ) // this is the biggest unit available
96  break;
97  }
98 
99  while( m_base != 0.0 && std::fabs( m_base ) < 1.000 )
100  {
101  m_base *= 1000.0;
102  m_prefix = (UNIT_PREFIX)( m_prefix - 3 );
103 
104  if( m_prefix == PFX_FEMTO ) // this is the smallest unit available
105  break;
106  }
107 }
double m_base
Definition: spice_value.h:139
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140

References m_base, m_prefix, PFX_FEMTO, and PFX_TERA.

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

◆ operator *()

SPICE_VALUE SPICE_VALUE::operator * ( const SPICE_VALUE aOther) const

Definition at line 214 of file spice_value.cpp.

215 {
216  SPICE_VALUE res( m_base * aOther.m_base, (UNIT_PREFIX)( m_prefix + aOther.m_prefix ) );
217  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
218  res.Normalize();
219 
220  return res;
221 }
double m_base
Definition: spice_value.h:139
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:34
bool m_spiceStr
Definition: spice_value.h:143

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

◆ operator+()

SPICE_VALUE SPICE_VALUE::operator+ ( const SPICE_VALUE aOther) const

Definition at line 152 of file spice_value.cpp.

153 {
154  int prefixDiff = m_prefix - aOther.m_prefix;
155  SPICE_VALUE res;
156  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
157 
158  // Convert both numbers to a common prefix
159  if( prefixDiff > 0 )
160  {
161  // Switch to the aOther prefix
162  res.m_base = ( m_base * std::pow( 10, prefixDiff ) ) + aOther.m_base;
163  res.m_prefix = aOther.m_prefix;
164  }
165  else if( prefixDiff < 0 )
166  {
167  // Use the current prefix
168  res.m_base = m_base + ( aOther.m_base * std::pow( 10, -prefixDiff ) );
169  res.m_prefix = m_prefix;
170  }
171  else
172  {
173  res.m_base = m_base + aOther.m_base;
174  res.m_prefix = m_prefix; // == aOther.m_prefix
175  }
176 
177  res.Normalize();
178 
179  return res;
180 }
double m_base
Definition: spice_value.h:139
void Normalize()
Normalize the value.
Definition: spice_value.cpp:88
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:34
bool m_spiceStr
Definition: spice_value.h:143

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

◆ operator-()

SPICE_VALUE SPICE_VALUE::operator- ( const SPICE_VALUE aOther) const

Definition at line 183 of file spice_value.cpp.

184 {
185  int prefixDiff = m_prefix - aOther.m_prefix;
186  SPICE_VALUE res;
187  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
188 
189  // Convert both numbers to a common prefix
190  if( prefixDiff > 0 )
191  {
192  // Switch to the aOther prefix
193  res.m_base = m_base * std::pow( 10, prefixDiff ) - aOther.m_base;
194  res.m_prefix = aOther.m_prefix;
195  }
196  else if( prefixDiff < 0 )
197  {
198  // Use the current prefix
199  res.m_base = m_base - aOther.m_base * std::pow( 10, -prefixDiff );
200  res.m_prefix = m_prefix;
201  }
202  else
203  {
204  res.m_base = m_base - aOther.m_base;
205  res.m_prefix = m_prefix; // == aOther.m_prefix
206  }
207 
208  res.Normalize();
209 
210  return res;
211 }
double m_base
Definition: spice_value.h:139
void Normalize()
Normalize the value.
Definition: spice_value.cpp:88
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:34
bool m_spiceStr
Definition: spice_value.h:143

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

◆ operator/()

SPICE_VALUE SPICE_VALUE::operator/ ( const SPICE_VALUE aOther) const

Definition at line 224 of file spice_value.cpp.

225 {
226  SPICE_VALUE res( m_base / aOther.m_base, (UNIT_PREFIX)( m_prefix - aOther.m_prefix ) );
227  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
228  res.Normalize();
229 
230  return res;
231 }
double m_base
Definition: spice_value.h:139
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:34
bool m_spiceStr
Definition: spice_value.h:143

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

◆ operator<()

bool SPICE_VALUE::operator< ( const SPICE_VALUE aOther) const
inline

Definition at line 115 of file spice_value.h.

116  {
117  return this->ToDouble() < aOther.ToDouble();
118  }
double ToDouble() const

References ToDouble().

◆ operator<=()

bool SPICE_VALUE::operator<= ( const SPICE_VALUE aOther) const
inline

Definition at line 125 of file spice_value.h.

126  {
127  return ( *this == aOther || *this < aOther );
128  }

◆ operator==()

bool SPICE_VALUE::operator== ( const SPICE_VALUE aOther) const
inline

Definition at line 105 of file spice_value.h.

106  {
107  return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
108  }
double m_base
Definition: spice_value.h:139
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140

References m_base, and m_prefix.

◆ operator>()

bool SPICE_VALUE::operator> ( const SPICE_VALUE aOther) const
inline

Definition at line 110 of file spice_value.h.

111  {
112  return this->ToDouble() > aOther.ToDouble();
113  }
double ToDouble() const

References ToDouble().

◆ operator>=()

bool SPICE_VALUE::operator>= ( const SPICE_VALUE aOther) const
inline

Definition at line 120 of file spice_value.h.

121  {
122  return ( *this == aOther || *this > aOther );
123  }

◆ stripZeros()

void SPICE_VALUE::stripZeros ( wxString &  aString)
staticprivate

< Remove redundant zeros from the end of a string.

Definition at line 234 of file spice_value.cpp.

235 {
236  if ( aString.Find( ',' ) >= 0 || aString.Find( '.' ) >= 0 )
237  {
238  while( aString.EndsWith( '0' ) )
239  aString.RemoveLast();
240 
241  if( aString.EndsWith( '.' ) || aString.EndsWith( ',' ) )
242  aString.RemoveLast();
243  }
244 }

Referenced by ToSpiceString(), and ToString().

◆ ToDouble()

double SPICE_VALUE::ToDouble ( ) const

Definition at line 110 of file spice_value.cpp.

111 {
112  double res = m_base;
113 
114  if( m_prefix != PFX_NONE )
115  res *= std::pow( 10, (int) m_prefix );
116 
117  return res;
118 }
double m_base
Definition: spice_value.h:139
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140

References m_base, m_prefix, and PFX_NONE.

Referenced by operator<(), operator>(), DIALOG_SIM_SETTINGS::parseCommand(), and ToString().

◆ ToOrigString()

wxString SPICE_VALUE::ToOrigString ( ) const
inline

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

Definition at line 92 of file spice_value.h.

93  {
94  return m_spiceStr ? ToSpiceString() : ToString();
95  }
wxString ToSpiceString() const
Return string value in Spice format (e.g.
wxString ToString() const
Return string value as when converting double to string (e.g.
bool m_spiceStr
Definition: spice_value.h:143

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().

◆ ToSpiceString()

wxString SPICE_VALUE::ToSpiceString ( ) const

Return string value in Spice format (e.g.

123.3456789k).

Definition at line 129 of file spice_value.cpp.

130 {
131  wxString res = wxString::FromCDouble( m_base );
132  stripZeros( res );
133 
134  switch( m_prefix )
135  {
136  case PFX_FEMTO: res += "f"; break;
137  case PFX_PICO: res += "p"; break;
138  case PFX_NANO: res += "n"; break;
139  case PFX_MICRO: res += "u"; break;
140  case PFX_MILI: res += "m"; break;
141  case PFX_NONE: break;
142  case PFX_KILO: res += "k"; break;
143  case PFX_MEGA: res += "Meg"; break;
144  case PFX_GIGA: res += "G"; break;
145  case PFX_TERA: res += "T"; break;
146  }
147 
148  return res;
149 }
double m_base
Definition: spice_value.h:139
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
static void stripZeros(wxString &aString)
< Remove redundant zeros from the end of a string.

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_SIM_SETTINGS::parseCommand(), DIALOG_SPICE_MODEL::parsePowerSource(), ToOrigString(), and TUNER_SLIDER::updateValueText().

◆ ToString()

wxString SPICE_VALUE::ToString ( ) const

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

123456.789).

Definition at line 120 of file spice_value.cpp.

121 {
122  wxString res( wxString::Format( "%.3f", ToDouble() ) );
123  stripZeros( res );
124 
125  return res;
126 }
double ToDouble() const
static void stripZeros(wxString &aString)
< Remove redundant zeros from the end of a string.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

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

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

Member Data Documentation

◆ m_base

double SPICE_VALUE::m_base
private

◆ m_prefix

UNIT_PREFIX SPICE_VALUE::m_prefix
private

Was the value defined using the Spice notation?

Definition at line 140 of file spice_value.h.

Referenced by Normalize(), operator *(), operator+(), operator-(), operator/(), operator==(), SPICE_VALUE(), ToDouble(), and ToSpiceString().

◆ m_spiceStr

bool SPICE_VALUE::m_spiceStr
private

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