RE: [go-nuts] Rounding to k digits

2025-08-15 Thread 'Bushnell, Thomas' via golang-nuts
golang-nuts Subject: Re: [go-nuts] Rounding to k digits This message was sent by an external party. The only thing a binary computer can store is a bit. A bit is either 0 or 1, both are integers. Or we can say that it can only store booleans true and false, but that doesn't chan

Re: [go-nuts] Rounding to k digits

2025-08-15 Thread Jan Mercl
native” you mean “implemented by assembly instructions”, but > then, floating point are implemented by assembly instructions…) > > > > Thomas > > > > > > *From:* 'Brian Candler' via golang-nuts > *Sent:* Thursday, August 14, 2025 10:04 PM > *To:* golang-nu

RE: [go-nuts] Rounding to k digits

2025-08-15 Thread 'Bushnell, Thomas' via golang-nuts
then, floating point are implemented by assembly instructions…) Thomas From: 'Brian Candler' via golang-nuts Sent: Thursday, August 14, 2025 10:04 PM To: golang-nuts Subject: Re: [go-nuts] Rounding to k digits This message was sent by an external party. On Wednesday, 13 August 2025 a

Re: [go-nuts] Rounding to k digits

2025-08-14 Thread 'Brian Candler' via golang-nuts
On Wednesday, 13 August 2025 at 20:10:50 UTC+7 Peter Weinberger (温博格) wrote: The canonical reference on this is by Guy Steele, "How to Print Floating Point Numbers Accurately" See also: https://0.30004.com/ -- You received this message because you are subscribed to the Google Grou

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread Jason E. Aten
I'll add that I typically just use fmt.Sprintf("%v", x), which gives the shortest possible unambiguous string representation of the floating point value x. On Wednesday, August 13, 2025 at 11:33:40 PM UTC+1 Jason E. Aten wrote: > Hi Jochen, > > I think you can get what you want simply with the

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread Jason E. Aten
Hi Jochen, I think you can get what you want simply with the fmt package. Consider fmt.Sprintf("%0.5f", x), for example, if you wanted 5 decimal places. If you need to actually round a floating point number, rather than just displaying a certain number of places, the CockroachDB guys wrote a

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread robert engels
A floating point number cannot represent all possible real numbers - so when you “shift it back” the number you expect may not be possible. See this on why 0.1 cannot be represented. https://how.dev/answers/why-does-01-not-exist-in-floating-point > On Aug 13, 2025, at 7:55 AM, Jochen Voss wrot

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread Jochen Voss
ected from the properties of real numbers. > > Thomas > > -Original Message- > From: 'Peter Weinberger (温博格)' via golang-nuts > > Sent: Wednesday, August 13, 2025 2:09 PM > To: Alexander Ertli > Cc: robert engels ; Jochen Voss ; > golang-nuts > Subj

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread Jochen Voss
Dear Peter, Thanks for the reference! All the best, Jochen On Wednesday, 13 August 2025 at 14:10:50 UTC+1 Peter Weinberger (温博格) wrote: > The canonical reference on this is by Guy Steele, "How to Print > Floating Point Numbers Accurately" > > On Wed, Aug 13, 2025 at 9:07 AM 'Alexander Ertli' vi

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread Jochen Voss
Hi Alexander, Thanks, this is interesting. My original func Round(x float64, digits int) float64 { eps := math.Pow10(-digits) return math.Round(x/eps) * eps } leads to near-immediate fuzzing failures, whereas your func Round(x float64, digits int) float64 { scale := math.Pow(10, float64(dig

RE: [go-nuts] Rounding to k digits

2025-08-13 Thread 'Bushnell, Thomas' via golang-nuts
c: robert engels ; Jochen Voss ; golang-nuts Subject: Re: [go-nuts] Rounding to k digits This message was sent by an external party. The canonical reference on this is by Guy Steele, "How to Print Floating Point Numbers Accurately" On Wed, Aug 13, 2025 at 9:07 AM 'Alexander Ertli&#

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread 温博格
The canonical reference on this is by Guy Steele, "How to Print Floating Point Numbers Accurately" On Wed, Aug 13, 2025 at 9:07 AM 'Alexander Ertli' via golang-nuts wrote: > > Hi Jochen, > > I think it's possible with a trick. > > My first naive thought on how to solve this is to simply shift the

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread 'Alexander Ertli' via golang-nuts
Hi Jochen, I think it's possible with a trick. My first naive thought on how to solve this is to simply shift the decimal point over, do the rounding on the whole number, and then shift it back. import "math" // Round performs rounding by shifting the decimal, rounding, and shifting back. func Ro

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread Jochen Voss
Dear Robert, Thank you for your response. To make sure I understand: Are you saying Claude's Round() function does not work? (For which example?) Or are you saying it is impossible to do better than Claude's function? Many thanks, Jochen On Wednesday, 13 August 2025 at 13:52:08 UTC+1 robert

Re: [go-nuts] Rounding to k digits

2025-08-13 Thread robert engels
Read up on numerical analysis - what you are asking for is impossible :) You need to convert to a string, or use BCD/fixed place values - like github.com/robaho/fixed > On Aug 13, 2025, at 7:42 AM, Jochen Voss wrote: > > Dear all, > > I would like to define a function "func Round(x float64, d