Skip to content

Commit

Permalink
Handle number formatting correctly when unit is "m"
Browse files Browse the repository at this point in the history
since "1 m" is interpreted as "1 milli" when unit is "m" it
should be left out unless a prefix is present.
  • Loading branch information
in3otd committed Aug 12, 2017
1 parent ae0df7c commit 1f987be
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 96 deletions.
146 changes: 57 additions & 89 deletions qucs/qucs/dialogs/matchdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
{
componentstr += QString("<Pac P1 1 %2 -30 18 -26 0 1 \"1\" 1 \"%1\" 1 "
"\"0 dBm\" 0 \"1 GHz\" 0>\n")
.arg(misc::num2str(value, 3)+"Ohm") // reference impedance
.arg(misc::num2str(value, 3, "Ohm")) // reference impedance
.arg(x_pos);
componentstr += QString("<GND * 1 %1 0 0 0 0 0>\n").arg(x_pos);
wirestr += QString("<%1 -60 %1 -120>\n").arg(x_pos);
Expand All @@ -1647,7 +1647,7 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
x_pos += 100;
componentstr += QString("<Pac P2 1 %2 -30 18 -26 0 1 \"1\" 1 \"%1\" 1 "
"\"0 dBm\" 0 \"1 GHz\" 0>\n")
.arg(misc::num2str(value, 3)+"Ohm") // reference impedance
.arg(misc::num2str(value, 3, "Ohm")) // reference impedance
.arg(x_pos);
componentstr += QString("<GND * 1 %1 0 0 0 0 0>\n").arg(x_pos);
wirestr += QString("<%1 -60 %1 -120>\n").arg(x_pos); // Vertical wire
Expand All @@ -1664,8 +1664,7 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
x_pos += 200;
} else if (!tag.compare("LS")) // Series inductor
{
QString val = misc::num2str(value, 3) +
"H"; // Add suffix nH, uH - 3 significant digits
QString val = misc::num2str(value, 3, "H"); // Add prefix, unit - 3 significant digits
componentstr += QString("<L L1 1 %1 -120 -26 10 0 0 \"%2\" 1 "
" 0>\n")
.arg(x_pos + 60)
Expand All @@ -1683,8 +1682,7 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
x_pos += x_series;
} else if (!tag.compare("CS")) // Series capacitor
{
QString val = misc::num2str(value, 3) +
"F"; // Add suffix pF, nF - 3 significant digits
QString val = misc::num2str(value, 3, "F"); // Add prefix, unit - 3 significant digits
componentstr += QString("<C C1 1 %1 -120 -26 17 0 0 \"%2\" 1 "
" 0>\n")
.arg(x_pos + 60)
Expand All @@ -1702,8 +1700,7 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
x_pos += x_series;
} else if (!tag.compare("LP")) // Shunt inductor
{
QString val = misc::num2str(value, 3) +
"H"; // Add suffix nH, uH - 3 significant digits
QString val = misc::num2str(value, 3, "H"); // Add prefix, unit - 3 significant digits
componentstr += QString("<GND * 1 %1 0 0 0 0 0>\n").arg(x_pos);
componentstr += QString("<L L1 1 %1 -30 5 -20 0 1 \"%2\" 1 "
" 0>\n")
Expand All @@ -1721,8 +1718,7 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
x_pos += x_shunt;
} else if (!tag.compare("CP")) // Shunt capacitor
{
QString val = misc::num2str(value, 3) +
"F"; // Add suffix pF, nF - 3 significant digits
QString val = misc::num2str(value, 3, "F"); // Add prefix, unit - 3 significant digits
componentstr += QString("<GND * 1 %1 0 0 0 0 0>\n").arg(x_pos);
componentstr += QString("<C C1 1 %1 -30 15 -20 0 1 \"%2\" 1 "
" 0>\n")
Expand All @@ -1745,24 +1741,20 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
er = Substrate.er;
getMicrostrip(value, Freq, &Substrate, width, er);
QString val_width =
misc::num2str(width, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
misc::num2str(width, 3, "m"); // Add prefix, unit - 3 significant digits
QString val_length =
misc::num2str(value2 / sqrt(er), 3) +
"m"; // Add suffix mm, cm - 3 significant digits
misc::num2str(value2 / sqrt(er), 3, "m"); // Add prefix, unit - 3 significant digits
componentstr +=
QString("<MLIN MS1 1 %3 -120 -26 20 0 0 \"Sub1\" 1 \"%1\" 1 \"%2\" "
"1 \"Hammerstad\" 0 \"Kirschning\" 0 \"26.85\" 0>\n")
.arg(val_width)
.arg(val_length)
.arg(x_pos + 60);
} else {
QString val_impedance = misc::num2str(value, 3) +
"Ohm"; // Add a suffix (although rarely needed
// here) - 3 significant digits
QString val_length =
misc::num2str(value2, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
// Add a prefix (although rarely needed here) and unit - 3 significant digits
QString val_impedance = misc::num2str(value, 3, "Ohm");
// Add prefix, unit - 3 significant digits
QString val_length = misc::num2str(value2, 3, "m");
componentstr += QString("<TLIN Line1 1 %3 -120 -26 20 0 0 \"%1\" 1 "
"\"%2\" 1 \"0 dB\" 0 \"26.85\" 0>\n")
.arg(val_impedance)
Expand All @@ -1786,25 +1778,21 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
{
er = Substrate.er;
getMicrostrip(value, Freq, &Substrate, width, er);
QString val_width =
misc::num2str(width, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_length =
misc::num2str(value2 / sqrt(er), 3) +
"m"; // Add suffix mm, cm - 3 significant digits
// Add prefix, unit - 3 significant digits
QString val_width = misc::num2str(width, 3, "m");
// Add prefix, unit - 3 significant digits
QString val_length = misc::num2str(value2 / sqrt(er), 3, "m");
componentstr +=
QString("<MLIN MS1 1 %3 -180 30 -30 0 1 \"Sub1\" 1 \"%1\" 1 \"%2\" "
"1 \"Hammerstad\" 0 \"Kirschning\" 0 \"26.85\" 0>\n")
.arg(val_width)
.arg(val_length)
.arg(x_pos);
} else {
QString val_impedance = misc::num2str(value, 3) +
"Ohm"; // Add a suffix (although rarely needed
// here) - 3 significant digits
QString val_length =
misc::num2str(value2, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
// Add a prefix (although rarely needed here) and unit - 3 significant digits
QString val_impedance = misc::num2str(value, 3, "Ohm");
// Add prefix, unit - 3 significant digits
QString val_length = misc::num2str(value2, 3, "m");
componentstr += QString("<TLIN Line1 1 %3 -180 30 -30 0 1 \"%1\" 1 "
"\"%2\" 1 \"0 dB\" 0 \"26.85\" 0>\n")
.arg(val_impedance)
Expand All @@ -1828,25 +1816,21 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
{
er = Substrate.er;
getMicrostrip(value, Freq, &Substrate, width, er);
QString val_width =
misc::num2str(width, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_length =
misc::num2str(value2 / sqrt(er), 3) +
"m"; // Add suffix mm, cm - 3 significant digits
// Add suffix mm, cm - 3 significant digits
QString val_width = misc::num2str(width, 3, "m");
// Add suffix mm, cm - 3 significant digits
QString val_length = misc::num2str(value2 / sqrt(er), 3, "m");
componentstr +=
QString("<MLIN MS1 1 %3 -60 -26 30 0 1 \"Sub1\" 1 \"%1\" 1 \"%2\" "
"1 \"Hammerstad\" 0 \"Kirschning\" 0 \"26.85\" 0>\n")
.arg(val_width)
.arg(val_length)
.arg(x_pos);
} else {
QString val_impedance = misc::num2str(value, 3) +
"Ohm"; // Add a suffix (although rarely needed
// here) - 3 significant digits
QString val_length =
misc::num2str(value2, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
// Add a prefix (although rarely needed here) and unit - 3 significant digits
QString val_impedance = misc::num2str(value, 3, "Ohm");
// Add suffix mm, cm - 3 significant digits
QString val_length = misc::num2str(value2, 3, "m");
componentstr += QString("<TLIN Line1 1 %3 -60 -26 30 0 1 \"%1\" 1 "
"\"%2\" 1 \"0 dB\" 0 \"26.85\" 0>\n")
.arg(val_impedance)
Expand All @@ -1869,25 +1853,17 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
{
er = Substrate.er;
getMicrostrip(value, Freq, &Substrate, width, er);
QString val_width =
misc::num2str(width, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_length =
misc::num2str(value2 / sqrt(er), 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_width = misc::num2str(width, 3, "m");
QString val_length = misc::num2str(value2 / sqrt(er), 3, "m");
componentstr +=
QString("<MLIN MS1 1 %3 -180 30 -30 0 1 \"Sub1\" 1 \"%1\" 1 \"%2\" "
"1 \"Hammerstad\" 0 \"Kirschning\" 0 \"26.85\" 0>\n")
.arg(val_width)
.arg(val_length)
.arg(x_pos);
} else {
QString val_impedance = misc::num2str(value, 3) +
"Ohm"; // Add a suffix (although rarely needed
// here) - 3 significant digits
QString val_length =
misc::num2str(value2, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_impedance = misc::num2str(value, 3, "Ohm");
QString val_length = misc::num2str(value2, 3, "m");
componentstr += QString("<TLIN Line1 1 %3 -180 30 -30 0 1 \"%1\" 1 "
"\"%2\" 1 \"0 dB\" 0 \"26.85\" 0>\n")
.arg(val_impedance)
Expand All @@ -1912,25 +1888,17 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
{
er = Substrate.er;
getMicrostrip(value, Freq, &Substrate, width, er);
QString val_width =
misc::num2str(width, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_length =
misc::num2str(value2 / sqrt(er), 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_width = misc::num2str(width, 3, "m");
QString val_length = misc::num2str(value2 / sqrt(er), 3, "m");
componentstr +=
QString("<MLIN MS1 1 %3 -60 30 -30 0 1 \"Sub1\" 1 \"%1\" 1 \"%2\" "
"1 \"Hammerstad\" 0 \"Kirschning\" 0 \"26.85\" 0>\n")
.arg(val_width)
.arg(val_length)
.arg(x_pos);
} else {
QString val_impedance = misc::num2str(value, 3) +
"Ohm"; // Add a suffix (although rarely needed
// here) - 3 significant digits
QString val_length =
misc::num2str(value2, 3) +
"m"; // Add suffix mm, cm - 3 significant digits
QString val_impedance = misc::num2str(value, 3, "Ohm");
QString val_length = misc::num2str(value2, 3, "m");
componentstr += QString("<TLIN Line1 1 %3 -60 20 30 0 1 \"%1\" 1 "
"\"%2\" 1 \"0 dB\" 0 \"26.85\" 0>\n")
.arg(val_impedance)
Expand All @@ -1954,8 +1922,8 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
// cover 1 octave below and 1 above, user will adjust if needed...
double freq_start = Freq / 2.0;
double freq_stop = 2.0 *Freq;
QString val_freq_start = misc::num2str(freq_start, 3) + "Hz";
QString val_freq_stop = misc::num2str(freq_stop, 3) + "Hz";
QString val_freq_start = misc::num2str(freq_start, 3, "Hz");
QString val_freq_stop = misc::num2str(freq_stop, 3, "Hz");

componentstr +=
QString("<.SP SP1 1 0 100 0 67 0 0 \"lin\" 1 \"%1\" 1 \"%2\" 1 "
Expand All @@ -1977,9 +1945,9 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
x_pos += 100;
if ((RL > 1e-3) && (XL < -1e-3)) // R + C
{
QString val_Res = misc::num2str(RL, 3) + "Ohm";
QString val_Cap = misc::num2str(1 / (fabs(XL) * 2 * pi * Freq), 3) +
"F"; // Need to use abs() because XL < 0
QString val_Res = misc::num2str(RL, 3, "Ohm");
// Need to use abs() because XL < 0
QString val_Cap = misc::num2str(1 / (fabs(XL) * 2 * pi * Freq), 3, "F");
componentstr +=
QString(
"<R R1 1 %1 -30 15 -26 0 -1 \"%2\" 1 \"26.85\" 0 \"US\" 0>\n")
Expand All @@ -1989,16 +1957,16 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
.arg(x_pos)
.arg(val_Cap);
paintingstr +=
QString("<Text %1 50 12 #000000 0 \"%4-j%5 %2 @ %3Hz\">\n")
QString("<Text %1 50 12 #000000 0 \"%4-j%5 %2 @ %3\">\n")
.arg(x_pos)
.arg(QChar(0x2126))
.arg(misc::num2str(Freq, 3))
.arg(misc::num2str(Freq, 3, "Hz"))
.arg(RL)
.arg(fabs(XL));
} else if ((RL > 1e-3) && (XL > 1e-3)) // R + L
{
QString val_Res = misc::num2str(RL, 3) + "Ohm";
QString val_Ind = misc::num2str(XL / (2 * pi * Freq), 3) + "H";
QString val_Res = misc::num2str(RL, 3, "Ohm");
QString val_Ind = misc::num2str(XL / (2 * pi * Freq), 3, "H");
componentstr +=
QString(
"<R R1 1 %1 -30 15 -26 0 -1 \"%2\" 1 \"26.85\" 0 \"US\" 0>\n")
Expand All @@ -2008,58 +1976,58 @@ void MatchDialog::SchematicParser(QString laddercode, int &x_pos, double Freq,
.arg(x_pos)
.arg(val_Ind);
paintingstr +=
QString("<Text %1 50 12 #000000 0 \"%4+j%5 %2 @ %3Hz\">\n")
QString("<Text %1 50 12 #000000 0 \"%4+j%5 %2 @ %3\">\n")
.arg(x_pos)
.arg(QChar(0x2126))
.arg(misc::num2str(Freq, 3))
.arg(misc::num2str(Freq, 3, "Hz"))
.arg(RL)
.arg(XL);
} else if ((RL > 1e-3) && (fabs(XL) < 1e-3)) // R
{
QString val_Res = misc::num2str(RL, 3) + "Ohm";
QString val_Res = misc::num2str(RL, 3, "Ohm");
componentstr +=
QString(
"<R R1 1 %1 -30 15 -26 0 -1 \"%2\" 1 \"26.85\" 0 \"US\" 0>\n")
.arg(x_pos)
.arg(val_Res);
wirestr += QString("<%1 -60 %1 -120>\n").arg(x_pos); // Vertical wire
paintingstr += QString("<Text %1 50 12 #000000 0 \"%4 %2 @ %3Hz\">\n")
paintingstr += QString("<Text %1 50 12 #000000 0 \"%4 %2 @ %3\">\n")
.arg(x_pos)
.arg(QChar(0x2126))
.arg(misc::num2str(Freq, 3))
.arg(misc::num2str(Freq, 3, "Hz"))
.arg(Freq * 1e-9)
.arg(RL);
} else if ((RL < 1e-3) && (XL > 1e-3)) // L
{
QString val_Ind = misc::num2str(XL / (2 * pi * Freq), 3) + "H";
QString val_Ind = misc::num2str(XL / (2 * pi * Freq), 3, "H");
componentstr +=
QString(
"<L L1 1 %1 -30 15 -26 0 -1 \"%2\" 1 \"26.85\" 0 \"US\" 0>\n")
.arg(x_pos)
.arg(val_Ind);
wirestr += QString("<%1 -60 %1 -120>\n").arg(x_pos); // Vertical wire
paintingstr +=
QString("<Text %1 50 12 #000000 0 \"j%4 %2 @ %3Hz\">\n")
QString("<Text %1 50 12 #000000 0 \"j%4 %2 @ %3\">\n")
.arg(x_pos)
.arg(QChar(0x2126))
.arg(misc::num2str(Freq, 3))
.arg(misc::num2str(Freq, 3, "Hz"))
.arg(Freq * 1e-9)
.arg(XL);
} else if ((RL < 1e-3) && (XL < -1e-3)) // C
{
QString val_Cap = misc::num2str(1 / (fabs(XL) * 2 * pi * Freq), 3) +
"F"; // Need to use abs() because XL < 0
// Need to use abs() because XL < 0
QString val_Cap = misc::num2str(1 / (fabs(XL) * 2 * pi * Freq), 3, "F");
componentstr +=
QString(
"<C C1 1 %1 -30 15 -26 0 -1 \"%2\" 1 \"26.85\" 0 \"US\" 0>\n")
.arg(x_pos)
.arg(val_Cap);
wirestr += QString("<%1 -60 %1 -120>\n").arg(x_pos); // Vertical wire
paintingstr +=
QString("<Text %1 50 12 #000000 0 \"-j%4 %2 @ %3Hz\">\n")
QString("<Text %1 50 12 #000000 0 \"-j%4 %2 @ %3\">\n")
.arg(x_pos)
.arg(QChar(0x2126))
.arg(misc::num2str(Freq, 3))
.arg(misc::num2str(Freq, 3, "Hz"))
.arg(fabs(XL));
}
wirestr += QString("<%1 -120 %2 -120>\n")
Expand Down
22 changes: 16 additions & 6 deletions qucs/qucs/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,18 @@ void misc::str2num(const QString& s_, double& Number, QString& Unit, double& Fac
return;
}

// #########################################################################
QString misc::num2str(double Num, int Precision)
/**
* @brief format number using metric prefixes, optionally adding a unit
* @param Num number to format
* @param Precision number of significant digits in the output string
* @param unit unit to add after the metric prefix
*
* handles the special case "1 m" meaning "1 milli(meter)", so avoid adding the unit if this is "m"
*/
QString misc::num2str(double Num, int Precision, QString unit)
{
char c = 0;
QString c;
QString Str;
double cal = fabs(Num);
if(cal > 1e-20) {
cal = log10(cal) / 3.0;
Expand All @@ -217,11 +225,13 @@ QString misc::num2str(double Num, int Precision)
case 4: c = 'T'; break;
}

if(c) Num /= pow(10.0, double(3*Expo));
if (!c.isEmpty()) Num /= pow(10.0, double(3*Expo));
}

QString Str = QString::number(Num, 'g', Precision);
if(c) Str += c;
if (!c.isEmpty() or (unit != "m"))
Str = QString("%1 %2%3").arg(QString::number(Num, 'g', Precision)).arg(c).arg(unit);
else
Str = QString::number(Num, 'g', Precision);

return Str;
}
Expand Down
2 changes: 1 addition & 1 deletion qucs/qucs/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace misc {
QString complexRad (double, double, int Precision=3);
QString StringNum (double, char form='g', int Precision=3);
void str2num (const QString&, double&, QString&, double&);
QString num2str (double, int Precision=6);
QString num2str (double, int Precision=6, QString unit="");
QString StringNiceNum(double);
void convert2Unicode(QString&);
void convert2ASCII(QString&);
Expand Down

0 comments on commit 1f987be

Please sign in to comment.