I want to perform division of a 22 digit no. in lotus script.
Can anyone please tell me how to do it? I am not getting the correct result.
For e.g;
dim num as Double;
dim num1 as Double;
num=123456789989898976765;
num1 = num / 97;
but i am not getting the correct result in num1.
To satisfy the mathematician in me, I have to tell you that you're never going to get the "correct" answer, the division produces a number that has an infinite decimal, but I get what you're after, I think. The number you want is:
but the number you're getting is:
This is due to the lack of precision in the number format used by the language. The loss of precision doesn't occur when you do the division, it happens much sooner than that, as soon as you assign the constant to the variable. The number you want to store is:
but the number you're actually storing is:
This is what results in the wrong answer.
Since I don't know the Lotus Script environment, I will do two things; first, give you some code that will fix this particular problem, like so:
that you can then generalize to fit your needs. This code splits the division into two smaller divisions that the number storage format CAN handle and adds the remainder of the first division to the second, producing this result:
which isn't exact, but probably close enough, right? How you split the bigger number into fragments without loosing precision is left up two you. (Hint: use strings)
Second, I will point you to BigInt.js, a library for doing math with arbitrarily large integers in JavaScript. If you can include this library into your code, it is definitely the more economical way to go.
I hope one of these helps.
I wrote a script library in java to solve the problem and called that from my Lotus script agent:
Below is the code:
Lotus Script Agent Code:
The problem is that you're using floating point numbers for the division, which won't yield accurate results. Lotusscript doesn't have support for large numbers other than floats, so you should probably should find another way to do the calculation. If you can write your code in Java, for instance, you'd have better support for big numbers. Or, as another example shows, you can apparently handle this in javascript as well.
See here : http://jsfromhell.com/classes/bignumber