Re: [go-nuts] why math.Pow gives different results depending on exp type

2024-04-20 Thread Dominik Honnef
Also, Staticcheck catches this:

$ curl -Os https://go.dev/play/p/oIKGb_uyLb3.go
$ staticcheck oIKGb_uyLb3.go 
oIKGb_uyLb3.go:9:38: the integer division '1 / 13' results in zero (SA4025)

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/87cyqkm409.fsf%40honnef.co.


Re: [go-nuts] why math.Pow gives different results depending on exp type

2024-04-19 Thread Kurtis Rader
This has nothing to do with the math.Pow function. Dividing two ints (1/13)
is not the same as dividing a float by an int (1.0/13). Replace your two
`fmt.Printf()` calls with

   println(1 / 13)
   println(1.0 / 13)

and observe the difference. The fact that both `math.Pow` arguments are of
type `float64` does not affect the division operator. It only causes the
result of the division to be coerced to a `float64` if possible. See
https://go.dev/ref/spec#Conversions.

On Fri, Apr 19, 2024 at 10:52 PM DrGo  wrote:

> ```
> package main
>
> import (
> "fmt"
> "math"
> )
>
> func main() {
> fmt.Printf("%g\n", 1-(math.Pow(0.6, 1/13)))   //result=0
> fmt.Printf("%g\n", 1-(math.Pow(0.6, 1.0/13))) //
> result=0.038532272011602364
> }
> ```
> https://go.dev/play/p/oIKGb_uyLb3
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/245517a8-37f9-4ad4-bc42-a1204beb723fn%40googlegroups.com
> 
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD__eo%2B2PV8_LZYK4Z-%3Dhi_tKor1s2jomAMWPXWdcr%2BaLg%40mail.gmail.com.


Re: [go-nuts] why math.Pow gives different results depending on exp type

2024-04-19 Thread 'Dan Kortschak' via golang-nuts
On Fri, 2024-04-19 at 22:51 -0700, DrGo wrote:
> ```
> package main
> 
> import (
>   "fmt"
>   "math"
> )
> 
> func main() {
>   fmt.Printf("%g\n", 1-(math.Pow(0.6, 1/13)))   //result=0
>   fmt.Printf("%g\n", 1-(math.Pow(0.6, 1.0/13))) //
> result=0.038532272011602364
> }
> ```

1/13 is 0 since both parts of the expression are int, and so it
truncates. After this, it is converted to 0.0. So you have 0.6^0 which
is 1.

https://go.dev/play/p/tq2VAPIWXCy

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/57f6740ba2d3e8df8fd4e30402eabb241a6af81b.camel%40kortschak.io.


[go-nuts] why math.Pow gives different results depending on exp type

2024-04-19 Thread DrGo
```
package main

import (
"fmt"
"math"
)

func main() {
fmt.Printf("%g\n", 1-(math.Pow(0.6, 1/13)))   //result=0
fmt.Printf("%g\n", 1-(math.Pow(0.6, 1.0/13))) // result=0.038532272011602364
}
```
https://go.dev/play/p/oIKGb_uyLb3

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/245517a8-37f9-4ad4-bc42-a1204beb723fn%40googlegroups.com.