1
/***************************************************************************
2
* Copyright (c) 2013 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
#define _USE_MATH_DEFINES
31
#include <Base/Tools.h>
35
#include <boost/math/special_functions/fpclassify.hpp>
37
/** \defgroup Units Units system
39
\brief The quantities and units system enables FreeCAD to work transparently with many different
43
// suppress annoying warnings from generated source files
45
#pragma warning(disable : 4003)
46
#pragma warning(disable : 4018)
47
#pragma warning(disable : 4065)
48
#pragma warning(disable : 4273)
49
#pragma warning(disable : 4335) // disable MAC file format warning on VC
54
// ====== Static attributes =========================
56
int QuantityFormat::defaultDenominator = 8; // for 1/8"
59
QuantityFormat::QuantityFormat()
60
: option(OmitGroupSeparator | RejectGroupSeparator)
62
, precision(UnitsApi::getDecimals())
63
, denominator(defaultDenominator)
66
QuantityFormat::QuantityFormat(QuantityFormat::NumberFormat format, int decimals)
67
: option(OmitGroupSeparator | RejectGroupSeparator)
69
, precision(decimals < 0 ? UnitsApi::getDecimals() : decimals)
70
, denominator(defaultDenominator)
73
// ----------------------------------------------------------------------------
79
Quantity::Quantity(double value, const Unit& unit)
84
Quantity::Quantity(double value, const QString& unit)
88
this->myValue = value;
89
this->myUnit = Unit();
94
auto tmpQty = parse(unit);
95
this->myUnit = tmpQty.getUnit();
96
this->myValue = value * tmpQty.getValue();
98
catch (const Base::ParserError&) {
100
this->myUnit = Unit();
104
double Quantity::getValueAs(const Quantity& other) const
106
return myValue / other.getValue();
109
bool Quantity::operator==(const Quantity& that) const
111
return (this->myValue == that.myValue) && (this->myUnit == that.myUnit);
114
bool Quantity::operator!=(const Quantity& that) const
116
return !(*this == that);
119
bool Quantity::operator<(const Quantity& that) const
121
if (this->myUnit != that.myUnit) {
122
throw Base::UnitsMismatchError(
123
"Quantity::operator <(): quantities need to have same unit to compare");
126
return (this->myValue < that.myValue);
129
bool Quantity::operator>(const Quantity& that) const
131
if (this->myUnit != that.myUnit) {
132
throw Base::UnitsMismatchError(
133
"Quantity::operator >(): quantities need to have same unit to compare");
136
return (this->myValue > that.myValue);
139
bool Quantity::operator<=(const Quantity& that) const
141
if (this->myUnit != that.myUnit) {
142
throw Base::UnitsMismatchError(
143
"Quantity::operator <=(): quantities need to have same unit to compare");
146
return (this->myValue <= that.myValue);
149
bool Quantity::operator>=(const Quantity& that) const
151
if (this->myUnit != that.myUnit) {
152
throw Base::UnitsMismatchError(
153
"Quantity::operator >=(): quantities need to have same unit to compare");
156
return (this->myValue >= that.myValue);
159
Quantity Quantity::operator*(const Quantity& other) const
161
return Quantity(this->myValue * other.myValue, this->myUnit * other.myUnit);
164
Quantity Quantity::operator*(double factor) const
166
return Quantity(this->myValue * factor, this->myUnit);
169
Quantity Quantity::operator/(const Quantity& other) const
171
return Quantity(this->myValue / other.myValue, this->myUnit / other.myUnit);
174
Quantity Quantity::operator/(double factor) const
176
return Quantity(this->myValue / factor, this->myUnit);
179
Quantity Quantity::pow(const Quantity& other) const
181
if (!other.myUnit.isEmpty()) {
182
throw Base::UnitsMismatchError("Quantity::pow(): exponent must not have a unit");
185
return Quantity(std::pow(this->myValue, other.myValue),
186
this->myUnit.pow(static_cast<signed char>(other.myValue)));
189
Quantity Quantity::pow(double exp) const
191
return Quantity(std::pow(this->myValue, exp), this->myUnit.pow(exp));
194
Quantity Quantity::operator+(const Quantity& other) const
196
if (this->myUnit != other.myUnit) {
197
throw Base::UnitsMismatchError("Quantity::operator +(): Unit mismatch in plus operation");
200
return Quantity(this->myValue + other.myValue, this->myUnit);
203
Quantity& Quantity::operator+=(const Quantity& other)
205
if (this->myUnit != other.myUnit) {
206
throw Base::UnitsMismatchError("Quantity::operator +=(): Unit mismatch in plus operation");
209
myValue += other.myValue;
214
Quantity Quantity::operator-(const Quantity& other) const
216
if (this->myUnit != other.myUnit) {
217
throw Base::UnitsMismatchError("Quantity::operator -(): Unit mismatch in minus operation");
220
return Quantity(this->myValue - other.myValue, this->myUnit);
223
Quantity& Quantity::operator-=(const Quantity& other)
225
if (this->myUnit != other.myUnit) {
226
throw Base::UnitsMismatchError("Quantity::operator -=(): Unit mismatch in minus operation");
229
myValue -= other.myValue;
234
Quantity Quantity::operator-() const
236
return Quantity(-(this->myValue), this->myUnit);
239
QString Quantity::getUserString(double& factor, QString& unitString) const
241
return Base::UnitsApi::schemaTranslate(*this, factor, unitString);
244
QString Quantity::getUserString(UnitsSchema* schema, double& factor, QString& unitString) const
246
return schema->schemaTranslate(*this, factor, unitString);
249
QString Quantity::getSafeUserString() const
251
auto retString = getUserString();
252
if (Q_LIKELY(this->myValue != 0)) {
253
auto feedbackQty = parse(retString);
254
auto feedbackVal = feedbackQty.getValue();
255
if (feedbackVal == 0) {
256
retString = QStringLiteral("%1 %2").arg(this->myValue).arg(this->getUnit().getString());
260
QString::fromStdString(Base::Tools::escapeQuotesFromString(retString.toStdString()));
264
/// true if it has a number without a unit
265
bool Quantity::isDimensionless() const
267
return isValid() && myUnit.isEmpty();
270
/// true if it has a specific unit or no dimension.
271
bool Quantity::isDimensionlessOrUnit(const Unit& unit) const
273
return isDimensionless() || myUnit == unit;
276
// true if it has a number and a valid unit
277
bool Quantity::isQuantity() const
279
return isValid() && !myUnit.isEmpty();
282
// true if it has a number with or without a unit
283
bool Quantity::isValid() const
285
return !boost::math::isnan(myValue);
288
void Quantity::setInvalid()
290
myValue = std::numeric_limits<double>::quiet_NaN();
293
// === Predefined types =====================================================
295
const Quantity Quantity::NanoMetre(1.0e-6, Unit(1));
296
const Quantity Quantity::MicroMetre(1.0e-3, Unit(1));
297
const Quantity Quantity::MilliMetre(1.0, Unit(1));
298
const Quantity Quantity::CentiMetre(10.0, Unit(1));
299
const Quantity Quantity::DeciMetre(100.0, Unit(1));
300
const Quantity Quantity::Metre(1.0e3, Unit(1));
301
const Quantity Quantity::KiloMetre(1.0e6, Unit(1));
303
const Quantity Quantity::MilliLiter(1000.0, Unit(3));
304
const Quantity Quantity::Liter(1.0e6, Unit(3));
306
const Quantity Quantity::Hertz(1.0, Unit(0, 0, -1));
307
const Quantity Quantity::KiloHertz(1.0e3, Unit(0, 0, -1));
308
const Quantity Quantity::MegaHertz(1.0e6, Unit(0, 0, -1));
309
const Quantity Quantity::GigaHertz(1.0e9, Unit(0, 0, -1));
310
const Quantity Quantity::TeraHertz(1.0e12, Unit(0, 0, -1));
312
const Quantity Quantity::MicroGram(1.0e-9, Unit(0, 1));
313
const Quantity Quantity::MilliGram(1.0e-6, Unit(0, 1));
314
const Quantity Quantity::Gram(1.0e-3, Unit(0, 1));
315
const Quantity Quantity::KiloGram(1.0, Unit(0, 1));
316
const Quantity Quantity::Ton(1.0e3, Unit(0, 1));
318
const Quantity Quantity::Second(1.0, Unit(0, 0, 1));
319
const Quantity Quantity::Minute(60.0, Unit(0, 0, 1));
320
const Quantity Quantity::Hour(3600.0, Unit(0, 0, 1));
322
const Quantity Quantity::Ampere(1.0, Unit(0, 0, 0, 1));
323
const Quantity Quantity::MilliAmpere(0.001, Unit(0, 0, 0, 1));
324
const Quantity Quantity::KiloAmpere(1000.0, Unit(0, 0, 0, 1));
325
const Quantity Quantity::MegaAmpere(1.0e6, Unit(0, 0, 0, 1));
327
const Quantity Quantity::Kelvin(1.0, Unit(0, 0, 0, 0, 1));
328
const Quantity Quantity::MilliKelvin(0.001, Unit(0, 0, 0, 0, 1));
329
const Quantity Quantity::MicroKelvin(0.000001, Unit(0, 0, 0, 0, 1));
331
const Quantity Quantity::MilliMole(0.001, Unit(0, 0, 0, 0, 0, 1));
332
const Quantity Quantity::Mole(1.0, Unit(0, 0, 0, 0, 0, 1));
334
const Quantity Quantity::Candela(1.0, Unit(0, 0, 0, 0, 0, 0, 1));
336
const Quantity Quantity::Inch(25.4, Unit(1));
337
const Quantity Quantity::Foot(304.8, Unit(1));
338
const Quantity Quantity::Thou(0.0254, Unit(1));
339
const Quantity Quantity::Yard(914.4, Unit(1));
340
const Quantity Quantity::Mile(1609344.0, Unit(1));
342
const Quantity Quantity::MilePerHour(447.04, Unit(1, 0, -1));
343
const Quantity Quantity::SquareFoot(92903.04, Unit(2));
344
const Quantity Quantity::CubicFoot(28316846.592, Unit(3));
346
const Quantity Quantity::Pound(0.45359237, Unit(0, 1));
347
const Quantity Quantity::Ounce(0.0283495231, Unit(0, 1));
348
const Quantity Quantity::Stone(6.35029318, Unit(0, 1));
349
const Quantity Quantity::Hundredweights(50.80234544, Unit(0, 1));
351
const Quantity Quantity::PoundForce(4448.22, Unit(1, 1, -2)); // lbf are ~= 4.44822 Newton
353
const Quantity Quantity::Newton(1000.0, Unit(1, 1, -2)); // Newton (kg*m/s^2)
354
const Quantity Quantity::MilliNewton(1.0, Unit(1, 1, -2));
355
const Quantity Quantity::KiloNewton(1e+6, Unit(1, 1, -2));
356
const Quantity Quantity::MegaNewton(1e+9, Unit(1, 1, -2));
358
const Quantity Quantity::NewtonPerMeter(1.00, Unit(0, 1, -2)); // Newton per meter (N/m or kg/s^2)
359
const Quantity Quantity::MilliNewtonPerMeter(1e-3, Unit(0, 1, -2));
360
const Quantity Quantity::KiloNewtonPerMeter(1e3, Unit(0, 1, -2));
361
const Quantity Quantity::MegaNewtonPerMeter(1e6, Unit(0, 1, -2));
363
const Quantity Quantity::Pascal(0.001, Unit(-1, 1, -2)); // Pascal (kg/m/s^2 or N/m^2)
364
const Quantity Quantity::KiloPascal(1.00, Unit(-1, 1, -2));
365
const Quantity Quantity::MegaPascal(1000.0, Unit(-1, 1, -2));
366
const Quantity Quantity::GigaPascal(1e+6, Unit(-1, 1, -2));
368
const Quantity Quantity::MilliBar(0.1, Unit(-1, 1, -2));
369
const Quantity Quantity::Bar(100.0, Unit(-1, 1, -2)); // 1 bar = 100 kPa
372
Quantity::Torr(101.325 / 760.0,
373
Unit(-1, 1, -2)); // Torr is a defined fraction of Pascal (kg/m/s^2 or N/m^2)
375
Quantity::mTorr(0.101325 / 760.0,
376
Unit(-1, 1, -2)); // Torr is a defined fraction of Pascal (kg/m/s^2 or N/m^2)
378
Quantity::yTorr(0.000101325 / 760.0,
379
Unit(-1, 1, -2)); // Torr is a defined fraction of Pascal (kg/m/s^2 or N/m^2)
381
const Quantity Quantity::PSI(6.894744825494, Unit(-1, 1, -2)); // pounds/in^2
382
const Quantity Quantity::KSI(6894.744825494, Unit(-1, 1, -2)); // 1000 x pounds/in^2
383
const Quantity Quantity::MPSI(6894744.825494, Unit(-1, 1, -2)); // 1000 ksi
385
const Quantity Quantity::Watt(1e+6, Unit(2, 1, -3)); // Watt (kg*m^2/s^3)
386
const Quantity Quantity::MilliWatt(1e+3, Unit(2, 1, -3));
387
const Quantity Quantity::KiloWatt(1e+9, Unit(2, 1, -3));
388
const Quantity Quantity::VoltAmpere(1e+6, Unit(2, 1, -3)); // VoltAmpere (kg*m^2/s^3)
390
const Quantity Quantity::Volt(1e+6, Unit(2, 1, -3, -1)); // Volt (kg*m^2/A/s^3)
391
const Quantity Quantity::MilliVolt(1e+3, Unit(2, 1, -3, -1));
392
const Quantity Quantity::KiloVolt(1e+9, Unit(2, 1, -3, -1));
394
const Quantity Quantity::MegaSiemens(1.0, Unit(-2, -1, 3, 2));
395
const Quantity Quantity::KiloSiemens(1e-3, Unit(-2, -1, 3, 2));
396
const Quantity Quantity::Siemens(1e-6, Unit(-2, -1, 3, 2)); // Siemens (A^2*s^3/kg/m^2)
397
const Quantity Quantity::MilliSiemens(1e-9, Unit(-2, -1, 3, 2));
398
const Quantity Quantity::MicroSiemens(1e-12, Unit(-2, -1, 3, 2));
400
const Quantity Quantity::Ohm(1e+6, Unit(2, 1, -3, -2)); // Ohm (kg*m^2/A^2/s^3)
401
const Quantity Quantity::KiloOhm(1e+9, Unit(2, 1, -3, -2));
402
const Quantity Quantity::MegaOhm(1e+12, Unit(2, 1, -3, -2));
404
const Quantity Quantity::Coulomb(1.0, Unit(0, 0, 1, 1)); // Coulomb (A*s)
406
const Quantity Quantity::Tesla(1.0, Unit(0, 1, -2, -1)); // Tesla (kg/s^2/A)
407
const Quantity Quantity::Gauss(1e-4, Unit(0, 1, -2, -1)); // 1 G = 1e-4 T
409
const Quantity Quantity::Weber(1e6, Unit(2, 1, -2, -1)); // Weber (kg*m^2/s^2/A)
411
// disable Oersted because people need to input e.g. a field strength of
412
// 1 ampere per meter -> 1 A/m and not get the recalculation to Oersted
413
// const Quantity Quantity::Oersted(0.07957747, Unit(-1, 0, 0, 1));// Oersted (A/m)
415
const Quantity Quantity::PicoFarad(1e-18, Unit(-2, -1, 4, 2));
416
const Quantity Quantity::NanoFarad(1e-15, Unit(-2, -1, 4, 2));
417
const Quantity Quantity::MicroFarad(1e-12, Unit(-2, -1, 4, 2));
418
const Quantity Quantity::MilliFarad(1e-9, Unit(-2, -1, 4, 2));
419
const Quantity Quantity::Farad(1e-6, Unit(-2, -1, 4, 2)); // Farad (s^4*A^2/m^2/kg)
421
const Quantity Quantity::NanoHenry(1e-3, Unit(2, 1, -2, -2));
422
const Quantity Quantity::MicroHenry(1.0, Unit(2, 1, -2, -2));
423
const Quantity Quantity::MilliHenry(1e+3, Unit(2, 1, -2, -2));
424
const Quantity Quantity::Henry(1e+6, Unit(2, 1, -2, -2)); // Henry (kg*m^2/s^2/A^2)
426
const Quantity Quantity::Joule(1e+6, Unit(2, 1, -2)); // Joule (kg*m^2/s^2)
427
const Quantity Quantity::MilliJoule(1e+3, Unit(2, 1, -2));
428
const Quantity Quantity::KiloJoule(1e+9, Unit(2, 1, -2));
429
const Quantity Quantity::NewtonMeter(1e+6, Unit(2, 1, -2)); // Joule (kg*m^2/s^2)
430
const Quantity Quantity::VoltAmpereSecond(1e+6, Unit(2, 1, -2)); // Joule (kg*m^2/s^2)
431
const Quantity Quantity::WattSecond(1e+6, Unit(2, 1, -2)); // Joule (kg*m^2/s^2)
432
const Quantity Quantity::KiloWattHour(3.6e+12, Unit(2, 1, -2)); // 1 kWh = 3.6e6 J
433
const Quantity Quantity::ElectronVolt(1.602176634e-13, Unit(2, 1, -2)); // 1 eV = 1.602176634e-19 J
434
const Quantity Quantity::KiloElectronVolt(1.602176634e-10, Unit(2, 1, -2));
435
const Quantity Quantity::MegaElectronVolt(1.602176634e-7, Unit(2, 1, -2));
436
const Quantity Quantity::Calorie(4.1868e+6, Unit(2, 1, -2)); // 1 cal = 4.1868 J
437
const Quantity Quantity::KiloCalorie(4.1868e+9, Unit(2, 1, -2));
439
const Quantity Quantity::KMH(277.778, Unit(1, 0, -1)); // km/h
440
const Quantity Quantity::MPH(447.04, Unit(1, 0, -1)); // Mile/h
442
const Quantity Quantity::AngMinute(1.0 / 60.0, Unit(0, 0, 0, 0, 0, 0, 0, 1)); // angular minute
443
const Quantity Quantity::AngSecond(1.0 / 3600.0, Unit(0, 0, 0, 0, 0, 0, 0, 1)); // angular second
445
Quantity::Degree(1.0,
446
Unit(0, 0, 0, 0, 0, 0, 0, 1)); // degree (internal standard angle)
447
const Quantity Quantity::Radian(180 / M_PI, Unit(0, 0, 0, 0, 0, 0, 0, 1)); // radian
448
const Quantity Quantity::Gon(360.0 / 400.0, Unit(0, 0, 0, 0, 0, 0, 0, 1)); // gon
451
// === Parser & Scanner stuff ===============================================
453
// include the Scanner and the Parser for the 'Quantity's
458
/* helper function for tuning number strings with groups in a locale agnostic way... */
460
double num_change(char* yytext, char dez_delim, char grp_delim)
464
std::array<char, num> temp {};
466
for (char* ch = yytext; *ch != '\0'; ch++) {
467
// skip group delimiter
468
if (*ch == grp_delim) {
471
// check for a dez delimiter other then dot
472
if (*ch == dez_delim && dez_delim != '.') {
478
// check buffer overflow
486
ret_val = atof(temp.data());
491
#if defined(__clang__)
492
#pragma clang diagnostic push
493
#pragma clang diagnostic ignored "-Wmissing-noreturn"
497
void Quantity_yyerror(const char* errorinfo)
499
throw Base::ParserError(errorinfo);
502
#if defined(__clang__)
503
#pragma clang diagnostic pop
507
#if defined(__clang__)
508
#pragma clang diagnostic push
509
#pragma clang diagnostic ignored "-Wsign-compare"
510
#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
511
#elif defined(__GNUC__)
512
#pragma GCC diagnostic push
513
#pragma GCC diagnostic ignored "-Wsign-compare"
514
#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
517
namespace QuantityParser
521
#define YYINITDEPTH 20
522
// show parser the lexer method
523
#define yylex QuantityLexer
526
// Parser, defined in QuantityParser.y
528
#include "QuantityParser.c"
530
#ifndef DOXYGEN_SHOULD_SKIP_THIS
531
// Scanner, defined in QuantityParser.l
533
#include "QuantityLexer.c"
534
#endif // DOXYGEN_SHOULD_SKIP_THIS
535
} // namespace QuantityParser
537
#if defined(__clang__)
538
#pragma clang diagnostic pop
539
#elif defined(__GNUC__)
540
#pragma GCC diagnostic pop
543
Quantity Quantity::parse(const QString& string)
546
QuantityParser::YY_BUFFER_STATE my_string_buffer =
547
QuantityParser::yy_scan_string(string.toUtf8().data());
548
// set the global return variables
549
QuantResult = Quantity(DOUBLE_MIN);
551
QuantityParser::yyparse();
552
// free the scan buffer
553
QuantityParser::yy_delete_buffer(my_string_buffer);