Jump to content

PHP big number math

Judahnator

I am writing a script that does math with large numbers.

 

Before you say anything, I know PHP sucks. I would be using Python or something better, but here I really did not have much choice.

It has its place, but anyone who has worked with the language knows it needs to die in fire.

 

Anyways, I need to do math with big numbers. For example:

351955014 * 2771124747904149240 = 9.7531125e+26

 

I dont want that though, I would rather it be like this:

351955014 * 2771124747904149240 = 975311249444351316422289360

 

How can this be done?

~Judah

Link to comment
Share on other sites

Link to post
Share on other sites

Are you looking for a library to do it or do you want to implement it yourself?

 

Im shooting to make the system as lightweight as possible, so doing it myself would be best

~Judah

Link to comment
Share on other sites

Link to post
Share on other sites

Im shooting to make the system as lightweight as possible, so doing it myself would be best

 

I don't work with PHP so I'm not sure how much overhead you'd be adding with one of the libraries. It might be worth testing before you decide to roll your own implementation.

 

If you do decide to implement things yourself, you'll have to decide how you want to represent the data (ex: strings). Then you'll have to manually implement any functionality you need so you can work with that data. You can choose to do this with just functions, or you can go the object oriented approach and create a BigNum class.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't work with PHP so I'm not sure how much overhead you'd be adding with one of the libraries. It might be worth testing before you decide to roll your own implementation.

 

If you do decide to implement things yourself, you'll have to decide how you want to represent the data (ex: strings). Then you'll have to manually implement any functionality you need so you can work with that data. You can choose to do this with just functions, or you can go the object oriented approach and create a BigNum class.

 

I got an email back from a friend of mine who teaches Calc at a local university, Im going in later today.

He said there was often shortcuts in working with big numbers, which could be applied to what I am working on.

 

If it comes to it, im sure i can just use a library. It wont be the end of the world :D

~Judah

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×