〇、关于 math

GO 语言的 math 库是一个内置的标准库,其中包含了许多数学函数和常量,用于计算各种数学运算和统计学计算。

日常开发中,计算当然是少不了的,那么今天来梳理下备查。

一、测试示例

1.1 小数位的:Round-四舍五入、RoundToEven-四舍/五至偶数

func Round(x float64 ) float64、func RoundToEven(x float64 ) float64

如下测试代码和结果:

package main

import (
"fmt"
"math"
) func main() {
fmt.Printf("math.Round(3.4):%v\n", math.Round(3.4))
fmt.Printf("math.Round(3.5):%v\n", math.Round(3.5))
fmt.Printf("math.Round(3.6):%v\n", math.Round(3.6))
// fmt.Printf("math.RoundToEven(3.5): %f\n", math.RoundToEven(3.4))
// fmt.Printf("math.RoundToEven(4.5): %f\n", math.RoundToEven(4.4))
fmt.Printf("math.RoundToEven(3.5): %f\n", math.RoundToEven(3.5))
fmt.Printf("math.RoundToEven(4.5): %f\n", math.RoundToEven(4.5))
// %f 输出精度格式:%总位数.小数位数f // 默认为六位小数
}

1.2 小数位的:ceil-进一法、Floor-去尾法、Trunc-取整数部分

func Ceil(x float64 ) float64

func Floor(x float64 ) float64

func Trunc(x float64 ) float64

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Ceil(1.4))
fmt.Println(math.Ceil(1.8))
fmt.Println(math.Floor(1.4))
fmt.Println(math.Floor(1.8))
fmt.Println(math.Trunc(1.4))
fmt.Println(math.Trunc(1.8))
}

1.3 Mod-取余、取整、Remainder-距离整除差值、Modf-分别取整数和小数部分

先看下除法运算,结果的值的精度依赖于被除数。当被除数为整数,则结果自动取整;当被除数为 float32,结果默认为带七位小数的浮点数;若为 float64 则结果保留16 位小数。如下示例:

package main

import (
"fmt"
) func main() {
result_int := 8 / 3
result_float := float32(8) / 3
fmt.Printf("int : %v\n", result_int)
fmt.Printf("float: %v\n", result_float) float_a := 7.7 // 默认为:float64
float_b := 3.3
fmt.Println("a/b-32 :", float32(float_a)/float32(float_b)) // float32 默认保留 7 位
fmt.Println("a/b-64 :", float_a/float_b) // float64 默认保留 16 位
num2 := fmt.Sprintf("%.5f", float_a/float_b) // 手动指定保留 5 位
fmt.Println("指定保留5位小数:", num2)
}

取整和取余

package main

import (
"fmt"
) func main() {
// 取整
result_int := 8 / 3
fmt.Printf("%v\n", result_int) // 输出:2
// 取余
int_a := 7
int_b := 4
fmt.Println(int_a % int_b) // 输出:3 // 百分号 % 用来取余数,只能用于整数之间
}

采用func Mod(x, y float64 ) float64取余:

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Mod(7, 4))
fmt.Println(math.Mod(7.2, 4))
fmt.Println(math.Mod(7, 4.1))
fmt.Println(math.Mod(7.2, 4.1))
}

通过func Remainder(x, y float64) float64计算整除剩余值:

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Remainder(7, 4))
fmt.Println(math.Remainder(7.2, 4))
fmt.Println(math.Remainder(7, 4.1))
fmt.Println(math.Remainder(7.2, 4.1))
}

通过func Modf(f float64) (int float64, frac float64)取整数和小数部分:

package main

import (
"fmt"
"math"
) func main() {
intv, frac := math.Modf(3.14)
fmt.Println(intv)
fmt.Printf("%f", frac)
}

1.4 Max-最大值、Min-最小值

func Max(x, y float64 ) float64

func Min(x, y float64 ) float64

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Max(1.5, 4.5))
fmt.Println(math.Max(-1.5, -4.5))
fmt.Println(math.Min(1.5, 4.5))
fmt.Println(math.Min(-1.5, -4.5))
}

1.5 Dim-差的绝对值(标准差)

func Dim(x, y float64 ) float64

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Dim(-1.4, -4))
fmt.Println(math.Dim(-0.4, -40))
}

1.6 Abs-绝对值、Sqrt-平方根、Cbrt-立方根、Pow-数值的 n 次方、Pow10-10 的 n 次方

func Abs(x float64 ) float64

func Sqrt(x float64) float64

func Cbrt(x float64) float64

func Pow(x, y float64 ) float64

func Pow10(n int) float64

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Abs(-1.4))
fmt.Println(math.Sqrt(9))
fmt.Println(math.Cbrt(8))
fmt.Println(math.Pow(2, 2))
fmt.Println(math.Pow(2, 3))
fmt.Println(math.Pow10(2)) // 输出:100
}

1.7 NaN-取一个非数字的值、IsNaN-判断值是否为非数字

func NaN() float64

func IsNaN(f float64) (is bool)

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.IsNaN(-1.4))
nan := math.NaN()
fmt.Println(math.IsNaN(nan))
}

1.8 Signbit-判断值是负数,0 或正数返回 false

func Signbit(x float64) bool

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Signbit(-1))
fmt.Println(math.Signbit(0))
fmt.Println(math.Signbit(1))
}

二、math 中的数学常量

如下常用的数学常量示例:

package main

import (
"fmt"
"math"
) func main() {
fmt.Println(math.Pi)
fmt.Println(math.E)
fmt.Println(math.Inf(-1))
fmt.Println(math.Inf(0))
fmt.Println(math.NaN())
}

其他常用数学常量还有:

  • MaxInt8、MaxInt16、MaxInt32、MaxInt64:表示各种整数类型的最大值。
  • MinInt8、MinInt16、MinInt32、MinInt64:表示各种整数类型的最小值。
  • MaxFloat32、MaxFloat64:表示各种浮点数类型的最大值。
  • MinFloat32、MinFloat64:表示各种浮点数类型的最小值。

参考:https://golang.google.cn/pkg/math/

math 库中常用的数学运算和常量【GO 基础】的更多相关文章

  1. iOS开发中常用的数学函数

    iOS开发中常用的数学函数 /*---- 常用数学公式 ----*/ //指数运算 3^2 3^3 NSLog(,)); //result 9 NSLog(,)); //result 27 //开平方 ...

  2. C# Math 中的常用的数学运算

    〇.动态库 System.Math.dll 引入动态库 using System.Math;   Math 为通用数学函数.对数函数.三角函数等提供常数和静态方法,使用起来非常方便,下边简单列一下常用 ...

  3. Python numpy 中常用的数据运算

    Numpy 精通面向数组编程和思维方式是成为Python科学计算大牛的一大关键步骤.——<利用Python进行数据分析> Numpy(Numerical Python)是Python科学计 ...

  4. C++中常用的数学函数总结

    我们在C++程序设计的过程中往往会使用到一些数学函数,那么不同的数学运算要用到什么函数哪?大家可以参考我的总结如下: 首先引用到数学函数时一定要记得加函数头文件 #include<cmath&g ...

  5. Python 3标准库 第五章 数学运算

    第五章数学运算-----------------------上下文解释:编程时,我们一般也是先给程序定义一些前提(环境变量.描述环境变化的全局变量等),这些“前提”就是上文,然后再编写各功能模块的代码 ...

  6. java数学函数Math类中常用的方法

    Math类提供了常用的一些数学函数,如:三角函数.对数.指数等.一个数学公式如果想用代码表示,则可以将其拆分然后套用Math类下的方法即可. Math.abs(12.3);               ...

  7. Javascript中的函数数学运算

    1.Math函数与属性使用语法 Math.方法名(参数1,参数2,...); Math.属性; 说明 Math函数可以没有参数,比如Math.random()函数,或有多个参数,比如Math.max( ...

  8. C语言中math.h中常用的函数

    1.绝对值 ①函数原型: int abs(int x); 函数功能: 求整数x的绝对值 int number=-1234; abs(number); ②函数原型:double fabs(double ...

  9. 机器学习之numpy库中常用的函数介绍(一)

    1. mat() mat()与array的区别: mat是矩阵,数据必须是2维的,是array的子集,包含array的所有特性,所做的运算都是针对矩阵来进行的. array是数组,数据可以是多维的,所 ...

  10. python第二十课——math模块中常用的函数

    属性: e:自然数 pi:圆周率 函数: ceil():向上取整 floor():向下取整 sqrt():开平方根 radians():角度转弧度 degrees():弧度转角度 import mat ...

随机推荐

  1. 在DevExpress的GridView的列中,使用RepositoryItemSearchLookUpEdit控件实现产品列表信息的展示和选择

    有时候,我们为了方便,我们往往使用扩展函数的代码方式创建很多GridView的操作功能,如在随笔<在DevExpress中使用BandedGridView表格实现多行表头的处理>中介绍过多 ...

  2. 曲线艺术编程 coding curves 第七章 抛物线(Parabolas)

    抛物线 Parabolas 原作:Keith Peters https://www.bit-101.com/blog/2022/11/coding-curves/ 译者:池中物王二狗(sheldon) ...

  3. Java NIO原理 (Selector、Channel、Buffer、零拷贝、IO多路复用)

    系列文章目录和关于我 零丶背景 最近有很多想学的,像netty的使用.原理源码,但是苦于自己对于操作系统和nio了解不多,有点无从下手,遂学习之. 一丶网络io的过程 上图粗略描述了网络io的过程,了 ...

  4. 揭秘Spring依赖注入和SpEL表达式

    摘要:在本文中,我们深入探讨了Spring框架中的属性注入技术,包括setter注入.构造器注入.注解式属性注入,以及使用SpEL表达式进行属性注入. 本文分享自华为云社区<Spring高手之路 ...

  5. ChatGPT之问艺道:如何借助神级算法Prompt,让你轻松get到更高质量答案?

    摘要:本文由葡萄城技术团队编写,文章的内容借鉴于Ibrahim John的<The Art of Asking ChatGPT>(向ChatGPT提问的艺术). 前言 当今,ChatGPT ...

  6. Spring源码核心剖析

    前言 SpringAOP作为Spring最核心的能力之一,其重要性不言而喻.然后需要知道的是AOP并不只是Spring特有的功能,而是一种思想,一种通用的功能.而SpringAOP只是在AOP的基础上 ...

  7. 驱动开发:摘除InlineHook内核钩子

    在笔者上一篇文章<驱动开发:内核层InlineHook挂钩函数>中介绍了通过替换函数头部代码的方式实现Hook挂钩,对于ARK工具来说实现扫描与摘除InlineHook钩子也是最基本的功能 ...

  8. PostgreSQL 12 文档: 部分 II. SQL 语言

    部分 II. SQL 语言 这部份描述在PostgreSQL中SQL语言的使用.我们从描述SQL的一般语法开始,然后解释如何创建保存数据的结构.如何填充数据库以及如何查询它.中间的部分列出了在SQL命 ...

  9. IoTOS-v1.2.1接入J-IM(t-io)后台通知App

    IoTOS v1.2.1 一.登录页增加可修改轮播 登录页增加可修改数据轮播: 首页轮播图由背景图片.标题.介绍.按钮一.按钮二(可配置跳转地址打开方式)组合而成 二.登录页增加常用运营商平台& ...

  10. [HUBUCTF 2022 新生赛]simple_RE

    [HUBUCTF 2022 新生赛]simple_RE 查壳,64位 找main函数,F5查看伪代码,简单分析一下 int __cdecl main(int argc, const char **ar ...