0

I am using minimal-json to parse a JSON stream in Java. This framework has a number of methods to read a number form the stream: asInt(), asLong(), asFloat(), and asDouble(). Assuming that I don't know what is the numerical type of the element, what method gives me the best accuracy? I'm asking this because I fear that reading all elements using asDouble() can cause loss of information. Is this a wrong assumption?

vainolo
  • 6,907
  • 4
  • 24
  • 47
  • Why do you think double will cause loss of information? – hellboy Mar 30 '14 at 05:16
  • 1
    @hellboy Check the answers of [this[(http://stackoverflow.com/questions/1650505/what-is-the-inclusive-range-of-float-and-double-in-java) question to understand my fear. – vainolo Mar 30 '14 at 05:23

2 Answers2

0

Assuming that you know that the numbers aren't going to be massive, I would use a float because its a smaller memory size. the last response on this stackoverflow question is the reason I would say use float. But if you aren't certain use a double: float and double

Community
  • 1
  • 1
Snoop Dogg
  • 62
  • 3
0

If a 64-bit long is written as an integer, and is greater than 2**53 (IEEE double has a 52-bit mantissa), then reading it as a double will lose precision.

One could make an argument, though, that storing such a number in JSON is the writer's error, because Javascript does not have 64-bit integers, only doubles.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55