FAQ
This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and determining
the length of the remainer? I currently perform a round to six places and
afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2 --> Base10 conversion.

--

## Search Discussions

• at Jan 23, 2013 at 8:44 am ⇧ On Wed, Jan 23, 2013 at 4:36 PM, Johann Höchtl wrote:

This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and determining
the length of the remainer? I currently perform a round to six places and
afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2 --> Base10 conversion.

Is this true? I think only the reverse is true (e.g. 0.3 has no exact
[finite] base-2 representation,
but every base-2 number has exact base-10 representation)

--
• at Jan 23, 2013 at 9:51 am ⇧ Am Mittwoch, 23. Januar 2013 09:43:42 UTC+1 schrieb minux:

On Wed, Jan 23, 2013 at 4:36 PM, Johann Höchtl <johann....@gmail.com<javascript:>
wrote:
This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and determining
the length of the remainer? I currently perform a round to six places and
afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2 --> Base10 conversion.

Is this true? I think only the reverse is true (e.g. 0.3 has no exact
[finite] base-2 representation,
but every base-2 number has exact base-10 representation)
Good point, you are probably right. Which would make it even more doable to
identify a more efficient way to determine the required bits of the
remainder.

--
• at Jan 23, 2013 at 3:16 pm ⇧ On Wed, Jan 23, 2013 at 3:36 AM, Johann Höchtl wrote:

This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and determining
the length of the remainer? I currently perform a round to six places and
afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2 --> Base10 conversion.
I am not sure what you are asking. The answer is probably no, but can you
give an example?

Thanks.
Russ

--
• at Jan 23, 2013 at 3:29 pm ⇧ math.Modf maybe?

--
• at Jan 24, 2013 at 10:32 am ⇧ On 23.01.2013 16:10, Russ Cox wrote:
On Wed, Jan 23, 2013 at 3:36 AM, Johann Höchtl
wrote:

This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and
determining the length of the remainer? I currently perform a
round to six places and afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2 --> Base10 conversion.

I am not sure what you are asking. The answer is probably no, but can
you give an example?
I want to determine the "places" of a float reminder for the required
precision for geohash encoding. 23.4 takes less space than 23.0005 or
23.123456 .

23.4 --> 1 place
23.0005 -> 4 places
23.123456 --> 6 places

The most straight-forward way (for me) is to convert to string
fmt.Sprintf("%.*f", prec, val) but I am pretty sure there is a more
efficient way.

Thanks.
Russ
--
• at Jan 24, 2013 at 11:34 am ⇧ Since you want fixed precision I think it's enough for you to round
x*1e6 to the nearest integer n and check n%10, n%100 etc.

Given that coordinates are bounded the probability that you obtain a
wrong result doing so is extremely thin.

If you want to do it properly, the algorithm is equivalent to using
strconv.AppendFloat and it's already quite efficient.

2013/1/24, Johann Höchtl <johann.hoechtl@gmail.com>:
On 23.01.2013 16:10, Russ Cox wrote:
On Wed, Jan 23, 2013 at 3:36 AM, Johann Höchtl
wrote:

This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and
determining the length of the remainer? I currently perform a
round to six places and afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2
--> Base10 conversion.

I am not sure what you are asking. The answer is probably no, but can
you give an example?
I want to determine the "places" of a float reminder for the required
precision for geohash encoding. 23.4 takes less space than 23.0005 or
23.123456 .

23.4 --> 1 place
23.0005 -> 4 places
23.123456 --> 6 places

The most straight-forward way (for me) is to convert to string
fmt.Sprintf("%.*f", prec, val) but I am pretty sure there is a more
efficient way.

Thanks.
Russ
--

--
• at Jan 24, 2013 at 10:00 pm ⇧ Have you seen this? As I understand, the same qustion, difgerent (better) answer (math.Float64bits).
If I did misunerstood you, than sorry for the noise.

GT

--
• at Jan 25, 2013 at 10:12 am ⇧ On 24.01.2013 23:00, Tamás Gulácsi wrote:
Have you seen this? As I understand, the same qustion, difgerent (better) answer (math.Float64bits).
If I did misunerstood you, than sorry for the noise.
I require far less precision, so going Float64bits would be overkill in my case. But certainly a valid way to do it.

Johann

GT

--
• at Jan 25, 2013 at 10:07 am ⇧ On 24.01.2013 12:34, Rémy Oudompheng wrote:
Since you want fixed precision I think it's enough for you to round
x*1e6 to the nearest integer n and check n%10, n%100 etc.

Given that coordinates are bounded the probability that you obtain a
wrong result doing so is extremely thin.
As precision is fixed to a maximum anyway (in this case), that's IMHO
the best and fastest solution to this problem, indeed. Thank you!
If you want to do it properly, the algorithm is equivalent to using
strconv.AppendFloat and it's already quite efficient.

2013/1/24, Johann Höchtl <johann.hoechtl@gmail.com>:
On 23.01.2013 16:10, Russ Cox wrote:
On Wed, Jan 23, 2013 at 3:36 AM, Johann Höchtl
wrote:

This question is only mildly Go - related.

Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and
determining the length of the remainer? I currently perform a
round to six places and afterwards perform a

fmt.Sprintf("%.*f", prec, val) but that seems highly inefficient.

I am aware that there are numbers for which there is no exact Base2
--> Base10 conversion.

I am not sure what you are asking. The answer is probably no, but can
you give an example?
I want to determine the "places" of a float reminder for the required
precision for geohash encoding. 23.4 takes less space than 23.0005 or
23.123456 .

23.4 --> 1 place
23.0005 -> 4 places
23.123456 --> 6 places

The most straight-forward way (for me) is to convert to string
fmt.Sprintf("%.*f", prec, val) but I am pretty sure there is a more
efficient way.

Thanks.
Russ
--

--
• at Jan 23, 2013 at 3:36 pm ⇧ On Wed, Jan 23, 2013 at 6:36 AM, Johann Höchtl wrote:
Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and determining
the length of the remainer? I currently perform a round to six places and
afterwards perform a
The "Printing Floating-Point Numbers Quickly and Accurately" paper
describes an algorithm for converting floating point numbers to
decimal numbers quickly, succinctly, and correctly. Should be easy to
adapt it for what you want.

gustavo @ http://niemeyer.net

--
• at Jan 23, 2013 at 5:53 pm ⇧ strconv.FormatFloat is already an implementation of that algorithm.

Rémy.

2013/1/23, Gustavo Niemeyer <gustavo@niemeyer.net>:
On Wed, Jan 23, 2013 at 6:36 AM, Johann Höchtl wrote:
Is there a more efficient way to determine the places after comma
(remainder) of a float64 rather than converting to string and determining
the length of the remainer? I currently perform a round to six places and
afterwards perform a
The "Printing Floating-Point Numbers Quickly and Accurately" paper
describes an algorithm for converting floating point numbers to
decimal numbers quickly, succinctly, and correctly. Should be easy to
adapt it for what you want.

gustavo @ http://niemeyer.net

--

--