-1

i have a function to calculate area of polygon, and here's my function

import {distance} from "mathjs"
function getArea(arrayCord) {
    let triangle =[]
    let area = 0.0
    let a =0.0
    let b =0.0
    let c =0.0
    let s =0.0
    for (let i = 0; i < arrayCord.length-2; i++) {
        a = distance(arrayCord[i],arrayCord[i+1])// p1-p2
        b = distance(arrayCord[i+1],arrayCord[i+2]) //p2 - p3
        c = distance(arrayCord[i],arrayCord[i+2]) //p3-p1
        s = (a+b+c)/2;
        triangle[i] = Math.sqrt(s*(s-a)*(s-b)*(s-c))
        area+= triangle[i]
    }
    return area;
}

when i copy the function into a typescript class and use it, i got this error that said

Operator '+' cannot be applied to types 'number | math.BigNumber' and 'number | math.BigNumber'

enter image description here

the function in my typescript was


          const getArea = (arryy:(number|undefined)[][]) =>{
            let triangle =[]
            let area = 0.0
            let a:(number|BigNumber) =0.0
            let b:(number|BigNumber) =0.0
            let c:(number|BigNumber) =0.0
            let s:(number|BigNumber) =0.0
            for (let i = 0; i < arryy.length-2; i++) {
              a = distance(arryy[i],arryy[i+1])// p1-p2
              b = distance(arryy[i+1],arryy[i+2]) //p2 - p3
              c = distance(arryy[i],arryy[i+2]) //p3-p1
              s = (a+b+c)/2;
              triangle[i] = Math.sqrt(s*(s-a)*(s-b)*(s-c))
              area+= triangle[i]
            }
            return area;
        }

how to solve this problem so that i can use my function on my typescript class?

deb97
  • 61
  • 1
  • 6
  • You have to convert the number to BigNumber before you add them. So do a type check, then add when both are guaranteed to be the same type. – Steven Spungin Dec 24 '21 at 00:20
  • how to do the conversion process? – deb97 Dec 24 '21 at 00:23
  • 1
    To convert from and to `BigNumber` see the [conversion section of the API](https://mathjs.org/docs/datatypes/bignumbers.html#conversion) – Ruan Mendes Dec 24 '21 at 00:31
  • 2
    Why are you adding `BigNumber` to your TS snippets? You don't seem to be using `BigNumber` If you need run calculations on them, you can't use `+`, you have to call `math.add` – Ruan Mendes Dec 24 '21 at 00:32

2 Answers2

0

You cannot use regular arithmetic on BigNumbers.

Replace

s = (a+b+c)/2

with

s = math.divide(math.add(a, b, c), 2)

But you need to beware that math.divide returns number | BigNumber | Fraction | Complex | Unit | Array | Matrix so your s may need to have its type adjusted.

See:

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
-1

number and BigNumber are seen as two data types, and Typescript doesn't know this addition operation is permitted. So in your case, a could be a number and b could be a BigNumber.

If you know for sure these can be added together, you can use // @ts-ignore I'm sure this adds on the line above the comment where error is shown.

David Min
  • 1,246
  • 8
  • 27
  • 2
    Suggesting to ignore TS's warning makes me cringe. This should be a comment, you're not really answering the question but offering a workaround that assumes something that is likely not true (you can't add numbers and BigNumbers with `+`) – Ruan Mendes Dec 24 '21 at 00:30
  • To each their own. If she finds it more useful to spend her time trying to get with the quirks of TS, however illogical or inconvenient for the sake of TS purity then your answer works great. If she might find this quirk a waste of her time, @ts-ignore is a complerely valid way for her to object to the way TS logic works. – David Min Dec 25 '21 at 22:45