Using BigInteger

J

Jeremy Watts

I've been trying to use the 'BigInteger' feature with no success - cant seem
to find any examples of code that for instance adds or subtracts two big
integer numbers.

How do you actually use BigInteger to do arithmetic on large integer
numbers?? Could someone give me some code examples please

thanks
 
B

Babu Kalakrishnan

Jeremy said:
I've been trying to use the 'BigInteger' feature with no success - cant seem
to find any examples of code that for instance adds or subtracts two big
integer numbers.

How do you actually use BigInteger to do arithmetic on large integer
numbers?? Could someone give me some code examples please

All operations on BigInteger numbers must be performed using the methods
provided (such as add, subtract, multiply etc) - You cannot write them
as expressions. e.g. If you want to add a and b which are BigIntegers,
you have to do

BigInteger c = a.add(b);

Look at the API docs for the methods available.

BK
 
T

Tim Slattery

Jeremy Watts said:
I've been trying to use the 'BigInteger' feature with no success - cant seem
to find any examples of code that for instance adds or subtracts two big
integer numbers.

How do you actually use BigInteger to do arithmetic on large integer
numbers?? Could someone give me some code examples please

BigInteger bi1 = new BigInteger(5);
BigInteger bi2 = new BigInteger(10);

BigInteger bi3 = bi1.add(bi2);
/* bi3 now contains the value 15 */

BigInteger bi4 = bi3.divide(bi1);
/* bi4 contains the value 3 */
 
B

Babu Kalakrishnan

Babu said:
All operations on BigInteger numbers must be performed using the methods
provided (such as add, subtract, multiply etc) - You cannot write them
as expressions. e.g. If you want to add a and b which are BigIntegers,
you have to do

BigInteger c = a.add(b);

Look at the API docs for the methods available.

Another thing to keep in mind is that BigInteger objects are immutable.
So even though you might naively think that a.add(b) would add the value
of b to a and hold the result in a, it does not work that way - you must
use the BigInteger object returned for the result.

BK
 
E

Eric Sosman

Tim said:
BigInteger bi1 = new BigInteger(5);

Where did you find that constructor? ITYM either

BigInteger bi1 = new BigInteger("5");
or
BigInteger bi1 = BigInteger.valueOf(5);
 
T

Thomas G. Marshall

Babu Kalakrishnan coughed up:
Another thing to keep in mind is that BigInteger objects are
immutable. So even though you might naively think that a.add(b) would
add the value of b to a and hold the result in a, it does not work
that way - you must use the BigInteger object returned for the result.

.....which BTW I think is sensible. A BigInteger of 5 is like the number 5.
You shouldn't (at least in java) be able to say 5.add(4), and have 5 be 9.

BTW, does smalltalk allow that?
 
J

Jeremy Watts

Jeremy Watts said:
I've been trying to use the 'BigInteger' feature with no success - cant
seem to find any examples of code that for instance adds or subtracts two
big integer numbers.

How do you actually use BigInteger to do arithmetic on large integer
numbers?? Could someone give me some code examples please

thanks

thanks all for your replies,

but i'm still getting probs getting any results from big_integer. do you
have to 'declare' the use of big_integer at the start or something? sorry
i'm a complete java newbie, but i've seen program snippets with 'import'
statements at the start. does big_integer have to be imported in some sense?

could someone please give me a complete applet that simply adds two
big_integer numbers??

thanks
 
S

Stefan Schulz

thanks all for your replies,

but i'm still getting probs getting any results from big_integer. do you
have to 'declare' the use of big_integer at the start or something?
sorry
i'm a complete java newbie, but i've seen program snippets with 'import'
statements at the start. does big_integer have to be imported in some
sense?

could someone please give me a complete applet that simply adds two
big_integer numbers??

Consider this your one free completed assignment ;)

import java.math.BigInteger;
// tells the compiler that we will be using the java.math.BigInteger class

public class Add2BigInts {
public static BigInteger add(BigInteger big1, BigInteger big2) {
return big1.add(big2);
}
}

See, it's not hard at all.

You may find the javadoc on BigInteger helpfull. The API documentation may
be d
ownloaded from Sun's website.
 
T

Tim Slattery

Eric Sosman said:
Where did you find that constructor? ITYM either

BigInteger bi1 = new BigInteger("5");
or
BigInteger bi1 = BigInteger.valueOf(5);

Guilty as charged. You have to supply a string, there's no BigInteger
constructor that takes an integer.
 
R

Robert

XProduct = A_BI.multiply(B_BI); //Multiply new values of A and B

if (A_BI.compareTo(B_BI) < 0) // Compare values of two BigInts -1 =
less than, 0 = equal to 1 = greater than

{
Insert code
}

Also in the examples below. The numbers should be treated as strings
and be in quotes when creating a new instance of a BigInteger.
BigInteger bi1 = new BigInteger("5");
BigInteger bi2 = new BigInteger("10");


Robert
 
B

Babu Kalakrishnan

Thomas said:
Babu Kalakrishnan coughed up:



....which BTW I think is sensible. A BigInteger of 5 is like the number 5.
You shouldn't (at least in java) be able to say 5.add(4), and have 5 be 9.

Yes - as long as you realize that BigInteger is immutable. After all, if
you create a new StringBuffer("SomeString") and call append(10) on it,
you can afford to throw away the returned StringBuffer object - can't we
? I've seen newbies apply the equivalence - that's why I posted the
clarification.
BTW, does smalltalk allow that?

Never used smalltalk - so wouldn't know.


BK
 
C

Chris Uppal

Thomas said:
....which BTW I think is sensible. A BigInteger of 5 is like the number
5. You shouldn't (at least in java) be able to say 5.add(4), and have 5
be 9.

BTW, does smalltalk allow that?

No, they are normally treated as immutable in Smalltalk. However they do
understand the normal arithmetic operations, so you write:

x := y + z.

just as for small integers.

(You /can/ modify them in place, but it's not good style to do so, and anyway
the arithmetic operations don't exist at that level -- you are just treating
them as arrays of bytes)

As an aside/advert, in Smalltalk all integers share a common superclass,
Integer, and the optimised (roughly equivalent to machine-native) SmallInteger
and unbounded LargeInteger are subclasses of that. In normal circumstances
they are treated completely uniformly (in fact portable code cannot assume the
existence of either subclass). One effect of that is that there are never any
truncation/wraparound/overflow issues in Smalltalk integer arithmetic[*]. One
of the things that I think the Java designers got wrong was not following that
pattern -- at least to the extent of making primitive arithmetical types an
/optimisation/ available to skilled programmers, rather than forcing them down
everybody's throat as the default.

[*] of course, Smalltalk is not the only "grown-up" language with this
property -- which makes the Java choice even less forgivable.

-- chris
 
T

Thomas G. Marshall

Babu Kalakrishnan coughed up:
Yes - as long as you realize that BigInteger is immutable.

You somehow missed that /that/ was what I was saying? A 5 /is/ immutable
(in most OOP's at least). As BigInteger's should be---At least I don't
consider it incorrect simply because primitive types (e.g. long) are not.
It was just a comment.

What's interesting is that I remember someone in c.o referring to a language
that allowed value mutation of what we might otherwise consider immediates.
As in:

5.setValue(6);
b=5+5;
(b is now 12)

I cannot find the post. Anyone know?



....[rip]...
 
Joined
Oct 26, 2011
Messages
1
Reaction score
0
Thanks for the help. Here's a gift!

public BigInteger factorial(BigInteger fIn){
BigInteger fOut = fIn;
BigInteger f = new BigInteger("1");
BigInteger g = new BigInteger("1");
BigInteger h = g.multiply(f);

for (int index = 1; index <= fIn.intValue(); index++){
f = new BigInteger(Integer.toString(index));
h = g.multiply(f);
g = h;

}
fOut = g;
System.out.println("PROBABILITY.FACTORIAL OUT : " + fOut.toString());
return fOut;
}:congrats:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top