Browse Source

Merge branch 'fixNumberRounding' of LInE/ivprog into master

Lucas de Souza 5 years ago
parent
commit
09019eee75
3 changed files with 33 additions and 31 deletions
  1. 9 9
      js/processor/ivprogProcessor.js
  2. 21 21
      js/processor/lib/math.js
  3. 3 1
      js/typeSystem/parsers.js

+ 9 - 9
js/processor/ivprogProcessor.js

@@ -884,9 +884,9 @@ export class IVProgProcessor {
           return new StoreObject(resultType, left.value.minus(right.value));
         case Operators.MULT.ord: {
           result = left.value.times(right.value);
-          if(result.dp() > Config.decimalPlaces) {
-            result = new Decimal(result.toFixed(Config.decimalPlaces));
-          }
+          // if(result.dp() > Config.decimalPlaces) {
+          //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+          // }
           return new StoreObject(resultType, result);
         }
         case Operators.DIV.ord: {
@@ -894,9 +894,9 @@ export class IVProgProcessor {
             result = left.value.divToInt(right.value);
           else
             result = left.value.div(right.value);
-          if(result.dp() > Config.decimalPlaces) {
-            result = new Decimal(result.toFixed(Config.decimalPlaces));
-          }
+          // if(result.dp() > Config.decimalPlaces) {
+          //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+          // }
           return new StoreObject(resultType, result);
         }
         case Operators.MOD.ord: {
@@ -908,9 +908,9 @@ export class IVProgProcessor {
             rightValue = rightValue.trunc();
           }
           result = leftValue.modulo(rightValue);
-          if(result.dp() > Config.decimalPlaces) {
-            result = new Decimal(result.toFixed(Config.decimalPlaces));
-          }
+          // if(result.dp() > Config.decimalPlaces) {
+          //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+          // }
           return new StoreObject(resultType, result);
         }          
         case Operators.GT.ord: {

+ 21 - 21
js/processor/lib/math.js

@@ -40,9 +40,9 @@ export function createSinFun () {
      } else {
        result = Decimal.sin(convertToRadians(angle));
      }
-     if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    //  if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
      const temp = new StoreObject(Types.REAL, result);
      sto.mode = Modes.RETURN;
      return Promise.resolve(sto.updateStore('$', temp));
@@ -68,9 +68,9 @@ export function createCosFun () {
       result = new Decimal(0)
     }
     result = Decimal.cos(convertToRadians(angle));
-    if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    // if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -91,9 +91,9 @@ export function createTanFun () {
       return Promise.reject("Tangent of "+x.value.toNumber()+"° is undefined.");
     }
     let result = Decimal.tan(convertToRadians(angle));
-    if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    // if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -110,9 +110,9 @@ export function createSqrtFun () {
   const sqrtFun = (sto, _) => {
     const x = sto.applyStore('x');
     let result = x.value.sqrt();
-    if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    // if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -130,9 +130,9 @@ export function createPowFun () {
     const x = sto.applyStore('x');
     const y = sto.applyStore('y');
     let result = x.value.pow(y.value);
-    if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    // if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -153,9 +153,9 @@ export function createLogFun () {
       return Promise.reject(new Error("the value passed to log function cannot be negative"));
     }
     let result = Decimal.log10(x.value);
-    if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    // if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));
@@ -205,9 +205,9 @@ export function createInvertFun () {
   const invertFun = (sto, _) => {
     const x = sto.applyStore('x');
     let result = toReal(1).dividedBy(x.value);
-    if(result.dp() > Config.decimalPlaces) {
-      result = new Decimal(result.toFixed(Config.decimalPlaces));
-    }
+    // if(result.dp() > Config.decimalPlaces) {
+    //   result = new Decimal(result.toFixed(Config.decimalPlaces));
+    // }
     const temp = new StoreObject(Types.REAL, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore('$', temp));

+ 3 - 1
js/typeSystem/parsers.js

@@ -1,6 +1,7 @@
 import { LanguageService } from "../services/languageService";
 import { Types } from "./types";
 import Decimal from "decimal.js";
+import { Config } from "../util/config";
 
 export function toInt (str) {
   return new Decimal(str);
@@ -50,6 +51,7 @@ export function convertBoolToString (bool) {
 }
 
 export function convertToString(value, type) {
+  console.log(value);
   switch (type.ord) {
     case Types.INTEGER.ord:
       return value.toString();
@@ -57,7 +59,7 @@ export function convertToString(value, type) {
       if (value.dp() <= 0) {
         return value.toFixed(1);  
       } else {
-        return value.toNumber();
+        return value.toFixed(Config.decimalPlaces);
       }
     }
     case Types.BOOLEAN.ord: