Perform division of a 22 digit number in lotus scr

2019-06-04 00:14发布

问题:

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.

回答1:

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:

1 272 750 412 266 999 760.463 917 525 ...

but the number you're getting is:

1 272 750 412 266 999 800

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:

123 456 789 989 898 976 765

but the number you're actually storing is:

123 456 789 989 898 980 000

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:

var num = [12345678998, 9898976765];
var num1 = num[0] / 97;
var num2 = Math.floor(num1);
num2 = num1 - num2;
num2 *= 97;
num2 = Math.round(num2)
num2 *= Math.pow(10, num[1].toString().length);
num2 = (num2 + num[1]) / 97;
alert(Math.floor(num1).toString() + num2.toString());

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:

1 272 750 412 266 999 760.463 917 7

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.



回答2:

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.



回答3:

See here : http://jsfromhell.com/classes/bignumber



回答4:

I wrote a script library in java to solve the problem and called that from my Lotus script agent:

Below is the code:

             //importing the required libraries
               import lotus.domino.*;
               import java.math.BigInteger;

              public class CalcProofNo
              {
              BigInteger BD_97 = new BigInteger("97");
              BigInteger BD_98 = new BigInteger("98");

              public  int calBBAN(String strBBAN) {
    //Gets the Basic Bank Account Number into BigInteger
    BigInteger BBAN = new BigInteger(strBBAN);

    //Calaculating Mod by dividing the Basic Bank Account Number by 97
    BBAN= BBAN.divideAndRemainder(BD_97)[1];

    //Subtracting 98 from the modulus
    BBAN = BD_98.subtract(BBAN);

    //Returning proof number in integer
    return BBAN.intValue();

              }    
         }

Lotus Script Agent Code:

     Option Public
     Use "CalcProofNo"
     Uselsx "*javacon"

Dim javaSession As JAVASESSION
Dim javaUtil As Variant
Dim javaUtilClass As Variant

      Set javaSession = New JAVASESSION

'Getting defined Class in ScriptLibrary 
Set javaUtilClass = javaSession.GetClass("CalcProofNo")

'Creating object for it 
Set javaUtil =javaUtilClass.CreateObject() 

' passing parameters to method, calling the method using object.
strProofNumber =   Cstr(javaUtil.calBBAN(strBBAN))