Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go math package has invalid result

Tags:

math

go

I just ran this code on my pc:

package main

import (
   "fmt"
   "math"
)

func main() {
   const ali = 4e20
   fmt.Println(math.Sin(ali))
}

and got this result:

1.3471173831553043e+258

Why Sin result goes more than 1?

Python and some other languages have correct result

like image 745
Moe Far Avatar asked Jul 30 '17 17:07

Moe Far


People also ask

What is the math package in go?

The math package in Go contains entirely of many math functions that can be used to do different things. Here are the main math functions that are used frequently. The most common math functions are as follows.

What is the use of math in Go language?

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. This function is used to return the absolute value of the specified number.

What are the functions included in the math package?

The math package contains many common functions that otherwise if implemented by the user may prove to be inefficient. 1. The Abs function The Abs function produces the absolute value of a float64 number. 2. The trigonometric functions The trigonometric functions are sin, cos, tan, their inverse and hyperbolic functions. 3. The log function

How to do modulus math with float64 data types in Golang?

To do modulus math with float64 data types, you’ll use the Mod function from the math package: In Go, as in mathematics, we need to keep in mind that operators will be evaluated in order of precedence, not from left to right or right to left.


1 Answers

It seems like it's just an issue with the implementation, if the input is more than 2**49 it causes issues. Found a really old issue here

like image 175
IanS5 Avatar answered Sep 28 '22 08:09

IanS5