aspose file tools*
The moose likes Beginning Java and the fly likes Math with array of bytes, need some guidance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Math with array of bytes, need some guidance" Watch "Math with array of bytes, need some guidance" New topic
Author

Math with array of bytes, need some guidance

D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
Hey -

I am trying to write a program that takes an input of a file formatted as such:
number1
number2
operator1
number3
operator2
etc, where after number1 there is a sequence of number -- operator. Then the math for doing it should be number 1 (operator 1) number 2 = new number
The problem is that these numbers are far too large to use an int, probably even a float once you get down to many operations including powers. So I would like to use an array of bytes in order to calculate the number correctly.

My problem is that I have absolutely no idea where to start. Can anybody give me any pointers?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Use java.math.BigInteger or java.math.BigDecimal.


[Jess in Action][AskingGoodQuestions]
D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
Homework assignment, can't. My professor for some reason thinks that making us do the hard things instead of using what's already available to us is the best way to teach us.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Obviously you are supposed to think through the algorithms you are using.

Try an int[] where each member of the array represents a decimal digit, and maybe a boolean flag for positive and negative. You can go through from right to left adding numbers at different positions and use the / and % operators to obtain the result and the carry. That's adding. Then work out how to do subtracting multiplying and dividing.

Powers are much more difficult; unless they are whole numbers, they use logs and cannot be readily implemented by this method. The quickest way to implement powers is probably to use a recursive method. It isn't difficult, but I can't remember just at the moment how to do it.
D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
He went over it a bit for the powers, so I'm not too worried about that - and I would use an int[] but he's already specified to use a byte[]. Oh well...

Thanks for the tip on % though.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You might be able to get some ideas from the Arbitrary-precision arithmetic wiki article


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
You can use bytes for the individual digits; each byte can hold from -128 to +127.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math with array of bytes, need some guidance
 
Similar Threads
InputStream.read(byte[])
arithmetic operators
Java2WSDL command inside WebSphere how to use it!
what does these bitwise shift doing here ??
Recursion Functions: The proper condition to stop it