Representing long decimals in hex in Java

  • Context: Java 
  • Thread starter Thread starter Paul Uszak
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The discussion centers on converting a long decimal number to hexadecimal in Java, specifically addressing the limitations of standard libraries like BigInteger and BigDecimal for this task. Participants recommend using the apfloat library for arbitrary precision floating-point arithmetic, as it supports base conversions. A sample Java code snippet demonstrates how to achieve this conversion by separating the integer and fractional parts of the decimal number. The conversation also touches on the need for clarity in the original poster's requirements regarding hexadecimal representation.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with BigInteger and BigDecimal classes in Java
  • Knowledge of arbitrary precision arithmetic concepts
  • Basic understanding of number base conversions (decimal to hexadecimal)
NEXT STEPS
  • Explore the apfloat library for arbitrary precision floating-point operations in Java
  • Learn about Java's BigInteger and BigDecimal classes for handling large numbers
  • Research algorithms for converting decimal numbers to hexadecimal format
  • Investigate the implications of floating-point precision in programming
USEFUL FOR

Java developers, software engineers working with numerical data, and anyone needing to perform high-precision arithmetic or conversions between number bases.

  • #31
I think that we both probably have the same pi file! Thanks for your code example. It's a clever idea that I hadn't thought of.

Thing is, my spider sense is tingling because I'm having a hard time understanding how the output can be pi like with less entropy that in the original (2.6 bits versus 3.3). My fear is that the code does not represent pi, but has generated a pseudo random file with characteristics determined exclusively by your code, and not by inherent mathematics. Is there any possibility that your code is acting as a pseudo random generator acting upon an ASCII pi seed?
 
Technology news on Phys.org
  • #32
The file generated by my code is determined by both - pi and the way in which my code does the conversion. There are many different ways to extract random bits out of pi. What's important here is that the output is perfectly random as long as the input is. The amount of information is reduced simply because some of it is thrown away in order to allow the algorithm to be faster.
 
  • #33
D H said:
one can, for example, calculate the quadrillionth (1015) hexadecimal digit of pi without having to calculate all of the 1015-1 hexadecimal digits that precede it.

I suspect that it's not quite as easy as you've written it. There's quite a summation to be calculated using arbitrary precision mathematics isn't there..?
 
  • #34
Filip Larsen said:
you can use the BigInteger class

I thought that this was the silver bullet. Wow, was I wrong :eek:

I used BigInteger and output 4MB of pi bytes. It worked perfectly and I've attached a piccy for anyone who wants to see what 3MB of pi looks like. BUT.

The BigInteger technique won't work in practice to process the 1GB file. It tool me 3 hours to process 4MB. I did some tests, and have attached a spreadsheet of my findings. It will take 4ish years to process the full 1GB file. This result is unexpected and not understood. I didn't even bother with memory requirements. It would actually be quicker to generate pi as bytes directly. I'll have to think of something else...
 

Attachments

  • 1700x1700-pi-bytes.png
    1700x1700-pi-bytes.png
    130.7 KB · Views: 496
  • pi-conversion.png
    pi-conversion.png
    37.3 KB · Views: 473
  • #35
What you are trying to do is ridiculous. The only property of π you are interested in is the randomness of its digits (in any base). The place value of its decimal digits is irrelevant and you have been given a perfectly good method of transforming the decimal digits to binary while preserving entropy (just reducing the overall volume of information), plus a pointer towards methods to generate π in binary directly if that is really what you want to do.

Did you really have to do all that work to realize that performing 109 multiplications of 109 digit numbers (which is what base conversion entails) is not computationally feasible?
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
Replies
17
Views
778
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
3
Views
4K