Skip to content

Commit

Permalink
fix ticket:4125, use mmc_(u|s)int_t instead of long!
Browse files Browse the repository at this point in the history
  • Loading branch information
adrpo authored and OpenModelica-Hudson committed Dec 19, 2016
1 parent 3f1dccb commit 5042c64
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Compiler/runtime/UnitParserExt_omc.cpp
Expand Up @@ -12,7 +12,7 @@ extern "C"

const char* UnitParserExt_unit2str(void *nums, void *denoms, void *tpnoms, void *tpdenoms, void *tpstrs)
{
long int i1,i2;
mmc_sint_t i1,i2;
string tpParam;
Unit unit;
unit.unitVec.clear();
Expand Down
24 changes: 11 additions & 13 deletions Compiler/runtime/unitparser.cpp
Expand Up @@ -33,8 +33,6 @@
#include <iostream>
#include <sstream>
#include <stack>
#include "omc_msvc.h" /* For round() */
#include "meta_modelica.h"

#ifndef NO_LPLIB

Expand All @@ -52,7 +50,7 @@
/* CLASS: Rational */
/***************************************************************************************/

Rational::Rational(long numerator, long denominator) {
Rational::Rational(mmc_sint_t numerator, mmc_sint_t denominator) {
num = numerator;
denom = denominator;
fixsign();
Expand All @@ -64,16 +62,16 @@ void Rational::rationalize(double r) {
#ifndef NO_LPLIB
const double eps = 1e-6;
double rapp;
long numerator = (long) r;
long denominator = 1;
mmc_sint_t numerator = (mmc_sint_t) r;
mmc_sint_t denominator = 1;
r = round(r / eps) * eps;
do {
rapp = (double) numerator / (double) denominator;
denominator *= 10;
numerator = (long) (r * denominator);
numerator = (mmc_sint_t) (r * denominator);
} while (fabs(r - rapp) > eps);

long d = gcd(numerator, denominator);
mmc_sint_t d = gcd(numerator, denominator);
num = numerator / d;
denom = denominator / d;
//cout << "Rationalized " << r << " to " << num << " / " << denom << endl;
Expand All @@ -90,7 +88,7 @@ bool Rational::isZero() {
return num == 0;
}

bool Rational::is(long numerator, long denominator) {
bool Rational::is(mmc_sint_t numerator, mmc_sint_t denominator) {
return (num == numerator) && (denom == denominator);
}

Expand Down Expand Up @@ -131,7 +129,7 @@ Rational Rational::div(Rational q1, Rational q2) {
}

Rational Rational::simplify(const Rational q) {
long gcd = Rational::gcd(q.num, q.denom);
mmc_sint_t gcd = Rational::gcd(q.num, q.denom);
Rational q2(Rational(q.num / gcd, q.denom / gcd));
q2.fixsign();
return q2;
Expand All @@ -144,9 +142,9 @@ void Rational::fixsign() {
}
}

long Rational::gcd(long a, long b) {
mmc_sint_t Rational::gcd(mmc_sint_t a, mmc_sint_t b) {
while (b != 0) {
long t = b;
mmc_sint_t t = b;
b = a % b;
a = t;
}
Expand Down Expand Up @@ -309,7 +307,7 @@ void UnitParser::addBase(const string quantityName, const string unitName,
u.quantityName = b.quantityName;
u.unitName = b.unitName;
u.unitSymbol = unitSymbol;
for (unsigned long j = 0; j < _base.size(); j++) {
for (mmc_uint_t j = 0; j < _base.size(); j++) {
u.unitVec.push_back(Rational((_base.size() - 1) == j ? 1 : 0));
}

Expand Down Expand Up @@ -994,7 +992,7 @@ UnitRes UnitParser::parseSymbol(Scanner& scan, Unit& unit) {
UnitRes UnitParser::parseRational(Scanner& scan, Rational& q) {

string str;
long l1, l2;
mmc_sint_t l1, l2;
Scanner::TokenType tok = scan.getToken(str);
if (tok == scan.TOK_INT) {
istringstream iss1(str);
Expand Down
14 changes: 8 additions & 6 deletions Compiler/runtime/unitparser.h
Expand Up @@ -37,18 +37,20 @@
#include <map>
#include <list>
#include <set>
#include "omc_msvc.h" /* For round() */
#include "meta_modelica.h"

using namespace std;

class Rational{
public:
Rational(long numerator=0, long denominator=1);
Rational(mmc_sint_t numerator=0, mmc_sint_t denominator=1);
Rational(const Rational& r);
virtual ~Rational(){;}
long num; //numerator
long denom; //denominator
mmc_sint_t num; //numerator
mmc_sint_t denom; //denominator
bool isZero();
bool is(long numerator, long denominator=1);
bool is(mmc_sint_t numerator, mmc_sint_t denominator=1);
string toString(); //e.g. "(7/9)". If denominator is one, only numerator is printed, e.g. "7".
double toReal();
void rationalize(double r);
Expand All @@ -59,7 +61,7 @@ class Rational{
static Rational add(Rational q1, Rational q2);
static Rational mul(Rational q1, Rational q2);
static Rational div(Rational q1, Rational q2);
static long gcd(long a, long b);
static mmc_sint_t gcd(mmc_sint_t a, mmc_sint_t b);

};

Expand Down Expand Up @@ -95,7 +97,7 @@ struct UnitRes{

class Unit{
public:
Unit(long pExp=0, long sFact=1, long off=0,double w = 1.0,bool b=false) :
Unit(mmc_sint_t pExp=0, mmc_sint_t sFact=1, mmc_sint_t off=0,double w = 1.0,bool b=false) :
prefixExpo(Rational(pExp)), scaleFactor(Rational(sFact)), offset(Rational(off)), prefixAllowed(true), weight(w) {;}

/** Vector stating exponents to the unit vector */
Expand Down

0 comments on commit 5042c64

Please sign in to comment.