Solved

Printing all the numbers.

Posted on 1998-07-19
17
173 Views
Last Modified: 2010-03-05
I've got a really large number and it always prints with an exponent.  I want it to print the whole number how do I do this?
e.g.
I input: 255255789012345678901234567890123456789
But if I divide it by one making it a number to PERL I get:  2.55255789012346e+038
I want: 255255789012345678901234567890123456789

Thanks in Advance.
0
Comment
Question by:cide
  • 7
  • 6
  • 2
  • +2
17 Comments
 

Author Comment

by:cide
ID: 1208491
Edited text of question
0
 
LVL 2

Expert Comment

by:mlev
ID: 1208492
I don't think Perl supports arbitrary precision arithmetics, does it?
0
 

Author Comment

by:cide
ID: 1208493
I have no idea.
0
 
LVL 8

Expert Comment

by:jbirk
ID: 1208494
Well....  You could always use an array of numbers, and a series of functions to load into the array, perform arithmetic if needed, and output the array...  I remember implementing a class for such an object in C++ as a class project...  although perl would be a bit more difficult.

-Josh
0
 

Author Comment

by:cide
ID: 1208495
I think there is probably another way.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208496
What operations do you need to perform on the number? If nothing but storing and printing, you could keep it as a string. If the only other operation you need is comparison (for sorting, for example) then that would be easy to implement as strign compares. Full math is harder. Perl keeps a finite number of digits of precision, and you've passed that number.

0
 
LVL 6

Expert Comment

by:alamo
ID: 1208497
Actually, it looks like Math::BigInt can handle arbitrary-sized numbers. It's a standard package you should already have. That's your solution.
0
 
LVL 6

Accepted Solution

by:
alamo earned 0 total points
ID: 1208498
Yes, Math:BigInt does the trick:

use Math::BigInt;

$i = new Math::BigInt "255255789012345678901234567890123456789";
$j = new Math::BigInt "2";
$k = $i*$j;
print $k;

prints
+510511578024691357802469135780246913578

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cide
ID: 1208499
It's having problems with NaN.  Not sure why.
$b = "104050051052053054055056057048049050051052053054055056057048049050051052053054055056057048049050051052053054055056057048";
use Math::BigInt;
$b = new Math::BigInt $b;
$b = substr($b,1,length($b)-1);
$c = $b**(1/54);
$d = new Math::BigInt int($c**54);
Any Idea why?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208500
substr does not return a bigint
(1/54) is not a bigint

0
 

Author Comment

by:cide
ID: 1208501
What exactly is a bigint then?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208502
use Math::BigInt;
$i = Math::BigInt->new($string);
print ref($i);
0
 

Author Comment

by:cide
ID: 1208503
How would I include this into my code then?
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208504
In order to do math with BigInts all operands need to be BigInts, as my original example showed.

To raise your $b to the 54th power, you need to first make a BigInt which contains the number 54:

$c54 = new Math::BigInt "54";
$d = $b**$c54;

But remember that BigInt is an integer - so 1/54 won't work. You can try BigFloat, but that doesn't seem to support **.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208505
You can convert a BigInt to a string with:

$str = "$b";
To print without the + then use
print substr($str,1);
0
 

Author Comment

by:cide
ID: 1208506
Well is there anyway to convert the number to bigint because it will do the 1/54 fine it's just when I try to **54 after that it gives me trouble.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208507
**54 works for me... but 1/54 doesn't, because a BigInt with the value 1/54 turns out to be 0. (It's the same as saying int(1/54).

You might be able to rearrange the operations so that integers work... I am not enough of a mathematician to know precisely how, though.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now