3 ms·
I do not understand why would you ever round trip floats through text. That seems useless? In any case, if you really want to do that then you use a hexadecim
by enriquto 6y ago
I do not understand why would you ever round trip floats through text. That seems useless? In any case, if you really want to do that then you use a hexadecimal float representation (like C's printf has had the "%a" conversion since forever) , or simply print out the bytes. Representing a float in decimal is absolutely idiotic and the people who commit this atrocity and get into trouble for that have it coming.
- ColinWright 6y ago> I do not understand why would you ever round trip floats through text. There are many, many occasions in the real world where this is necessary ... I've dealt with it multiple times. When done carefully and with appropriate knowledge of the underlying systems it's not a problem. But just because you've never done, that doesn't mean it's the wrong thing to be doing. > if you really want to do that then you use a hexadecimal float representation There are combinations of systems where that won't work.
- pjc50 6y agoJSON? Human-readable representations in general? > Representing a float in decimal is absolutely idiotic Other way round: humans like decimal arithmetic, and will accept binary floating point as an intermediate representation.
- dahfizz 6y agoWe are talking about two machines talking to each other. Correctness is more important than human readability for bytes on a wire.
- ColinWright 6y agoSometimes when two machines talk to each other there is no reliable way to do so in binary. Different endianness, different internal representation of floats, and so forth. So converting floats can sometimes be necessary, and that's what we're talking about. Going via human readable text is only one specific example.
- dahfizz 6y agoThat is an extreme edge case. The vast majority of computing happens on x86 and ARM. Unless you're working on an IBM mainframe, you're using a little endian, IEEE float machine.
- ColinWright 6y agoI have extensive experience of other systems, and for me, with my experience, it's not an extreme edge case. If for you it is an extreme edge case, then I suggest you put it to one side and hope it never becomes relevant. But if one day you find a bug you can't explain, maybe it will help you.
- enriquto 6y ago> Different endianness, different internal representation of floats, and so forth. But this has nothing at all to do with floats. If you transmit 16 or 32 bit ints, or any image/video/sound datatype, or even raw bytes, there can be endianness shenanigans. Introducing endianness when talking about floating point numbers is completely offtopic.
- ColinWright 6y agoIt has everything to do with internal representation of data in general, and floats are one specific example that turns up quite regularly. Sometimes one machine has some data, and you need to transfer that to another machine. Sometimes the internal representation of that data will be different. Sometimes you can't simply "send the bits" and have it work. So in those cases you need to go through an intermediate representation, and then the question of conversion arises. So sometimes, when sending a float (as a single example) from one machine to another, you have to go through conversions, and one way to do that is to generate a string representation. The point here and in other comments is that many people think this is easy and obvious, but in truth it's hard and full of pitfalls. You said: > I do not understand why would you ever round trip floats through text. I'm trying to explain that. I have some experience of the issues involved, and was trying to share my experience. You are dismissing it, which is entirely your choice, and in that case, my comments are here for others.
- dragonwriter 6y ago> We are talking about two machines talking to each other. There are allnkinds of reasons machines talk to each other in human readable formats. APIsare for machine-to-machine communications, and yet APIs that speak JSON or XML are not uncommon.
- kstenerud 6y agoUnfortunately, JSON doesn't have a mechanism for 100% accuracy in transmitting binary floating point numbers. Everything gets marshaled to decimal float, and then unmarshaled to binary float - even in machine-to-machine communication. That's simply a limitation of the format. It's part of the reason I developed Concise Encoding: https://github.com/kstenerud/concise-encoding/#example https://github.com/kstenerud/concise-encoding/#example which allows hex float notation when you need to be exact.
- enriquto 6y ago> Unfortunately, JSON doesn't have a mechanism for 100% accuracy in transmitting binary floating point numbers. If it was just inaccurate! It cannot even deal with straightforward floats like inf, nan, or minus zero!
- ColinWright 6y agoNot to flog unnecessarily an expired equine, you say: > C's printf has had the "%a" conversion since forever That turns out not to be the case. Would you care to guess: * Which year it was first introduced? * Which year it became wide-spread? * When I started programming? * How old some of the systems are in which I still work?