0

I would like to represent a double as a string, without any extra 0s or without any rounding losing any its significant digits. Ignoring minor errors due to the nature of floating point.. e.g. when it might have a bunch of 0s after the last really useful digit with a 3 tacked on the end.. e.g. 56.825000000003 (not sure if I got the number of 0s correct, just illustrating a point here...

The problem is I don't know how many digits after the decimal point are significant. I mean I know within reason.. for example 12.230000000005, with that many strings of 0s you know the 5 is is not going to be part of the "valid" number"

Examples of #s that I would like to acehived as a string but could have an extraneous digit at the end, being a double, could be 12, 12.25, 6.125

What would be a nice concise way of doing this?

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • 1
    Doing what, exactly? You don't seem to know *what* you want to be displayed, much less how to produce that result. – Scott Hunter Dec 27 '21 at 14:08
  • https://stackoverflow.com/questions/332111/how-do-i-convert-a-double-into-a-string-in-c Same question asked here/ – Smitha Kalluz Dec 27 '21 at 14:22
  • If you can't specify how to determine how many digits after the decimal point are significant, there is no feasible answer to your question. Remember, code is the language for the programmer to tell the compiler how things need to be done. The compiler is the ignoramus that pedantically interprets your code, and it relies on the programmer for instructions on what to do. If, as the programmer, you can't specify what you intend, you can't write code that directs the compiler. – Peter Dec 27 '21 at 14:30
  • Your “nice” examples all have power-of-two denominators and thus have no “garbage digits” regardless. Is that just an accident? – Davis Herring Dec 27 '21 at 14:49
  • The portable, correct way to do this would be to represent numbers as a reduced fraction, and only convert to decimal points on display (with base-10 long division in the background - you can't rely on double machine instructions to not muck it up). You'll be dealing with the values symbolically. – JohnFilleau Dec 27 '21 at 15:20
  • And what if your result is irrational? Should your machine print digits forever? You have the good start to a specification, but you're missing cases (not even edge cases). – JohnFilleau Dec 27 '21 at 15:21
  • You need to define what you mean by “significant” digits. The usual definition is the digits from the first non-zero digit to the last non-zero digit. However, you seem to be using it in a different way. With your example, 12.230000000005, you suggest 12.23 is the “valid” part of the number. But how would you know that? If an algorithm produces many million so numbers (using ideal real-number arithmetic for illustration), from time to time, some result will work out to something like 12.230000000005. You cannot know just from the number that somebody wants to chop part of it off. – Eric Postpischil Dec 27 '21 at 15:42
  • There are algorithms that converting a `double` number to **just enough** decimal digits so that the resulting number uniquely distinguishes the original `double`, in the sense that converting the number back to `double` (with rounding-to-nearest, ties-to-even) reproduces the original `double`. Those algorithms are not “nice concise“ ways of doing this; they are [the results of years of work](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Binary-to-decimal_conversion_with_minimal_number_of_digits). – Eric Postpischil Dec 27 '21 at 15:45
  • They are, however, built into JavaScript and Java, so calling an existing routine would be a good way to do it. – Eric Postpischil Dec 27 '21 at 15:46
  • ah yes there would be no irrational numbers – ycomp Dec 27 '21 at 15:59
  • I guess an easy way to make the problem simpler would be to have a parameter that says, more than this # of digits we can ignore.. e..g `maxSignificantDigits` - let's say 8 for example... so any 9th, 10th, etc. digit can be ignored. Anything to the left of that should be reduced to having trailing zeros removed. – ycomp Dec 27 '21 at 16:03
  • Well, what you describe in this last comment is precisely rounding to `maxSignificantDigits` decimal digits. – Igor Tandetnik Dec 28 '21 at 01:09

0 Answers0