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

Discussion Overview

The discussion revolves around how to represent a long decimal number in hexadecimal format using the Java programming language. Participants explore various methods, libraries, and algorithms that could facilitate this conversion, addressing both theoretical and practical aspects of the problem.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant asks how to convert a specific long decimal number to hexadecimal in Java.
  • Another participant suggests that the conversion is straightforward and emphasizes the need for an extended precision algorithm, indicating that standard math libraries may not suffice.
  • Some participants propose using the BigInteger and BigDecimal classes for handling the conversion, with specific methods for managing the decimal point.
  • There is a contention regarding the effectiveness of using BigInteger and BigDecimal, with one participant arguing that they do not support base conversions, suggesting instead the use of the apfloat library for arbitrary precision.
  • Several participants express uncertainty about the OP's exact requirements, questioning whether the focus should be on handling the decimal point or on the conversion itself.
  • One participant highlights the potential for a finite decimal number to have an infinite representation in hexadecimal, adding complexity to the discussion.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to convert the decimal number to hexadecimal. There are competing views on the appropriateness of using BigInteger and BigDecimal versus the apfloat library, and uncertainty remains regarding the OP's specific needs.

Contextual Notes

Participants note limitations in the standard Java libraries for handling hexadecimal floating-point representations, and there is ambiguity regarding the OP's intent and requirements for the conversion process.

Who May Find This Useful

This discussion may be of interest to Java programmers dealing with numerical representations, particularly those needing to convert long decimal numbers to hexadecimal format with considerations for precision and representation.

  • #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: 498
  • pi-conversion.png
    pi-conversion.png
    37.3 KB · Views: 475
  • #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
1K
  • · Replies 8 ·
Replies
8
Views
4K
  • · 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