zoukankan      html  css  js  c++  java
  • Math对象

    原文地址:https://wangdoc.com/javascript/

    静态属性

    Math对象的静态属性提供以下一些数学常数。

    • Math.E:常数e
    • Math.LN2:2的自然对数。
    • Math.LN10:10的自然对数。
    • Math.LOG2E:以2为底的e的对数。
    • Math.LOG10E:以10为底的e的对数。
    • Math.PI:常数π
    • Math.SQRT1_2:根号0.5。
    • Math.SQRT2:根号2。
      这些属性都是只读的,不能修改。

    静态方法

    Math对象提供以下静态方法。

    • Math.abs():绝对值
    • Math.ceil():向上取整
    • Math.floor():向下取整
    • Math.max():最大值
    • Math.min():最小值
    • Math.pow():指数运算
    • Math.sqrt():平方根
    • Math.log():自然对数
    • Math.exp()ede的指数
    • Math.round():四舍五入
    • Math.random():随机数

    Math.max(),Math.min()

    Math.max方法返回参数之中最大的那个值,Math.min方法返回参数之中最小的那个值。如果参数为空,Math.min返回InfinityMath.max返回-Infinity

    Math.floor(),Math.ceil()

    这两个方法可以结合起来,实现一个总是返回数组的整数部分的函数。

    function ToInteger(x) {
        x = Number(x);
        return x < 0 ? Math.ceil(x) : Math.floor(x);
    }
    
    ToInteger(3.2) // 3
    ToInteger(-3.2) // 3
    

    Math.round()

    Math.round方法四舍五入。

    Math.round(0.1); // 0
    Math.round(0.5); // 1
    
    //等同于
    Math.floor(x + 0.5);
    

    注意,它对负数的处理(主要是对0.5的处理)。

    Math.round(-1.5); // -1
    

    Math.sqrt()

    Math.sqrt方法返回参数值的平方根,如果参数为负数,则返回NaN

    Math.random()

    Math.random返回0到1之间的一个伪随机数,可能等于0,但是一定小于1。
    生成任意范围的随机数。

    function getRandomArbitrary(min, max) {
        return Math.random() * (max - min) + min;
    }
    
    getRandomArbitrart(1.5, 6.5);
    

    三角函数方法

    Math对象还提供一系列三角函数方法。

    • Math.sin():返回参数的正弦,参数为弧度。
    • Math.cos():返回参数的余弦,参数为弧度。
    • Math.tan():返回参数的正切,参数为弧度。
    • Math.asin():返回参数的反正弦,参数为弧度。
    • Math.acos():返回参数的反余弦,参数为弧度。
    • Math.atan():返回参数的反正切,参数为弧度。
  • 相关阅读:
    adodb.stream文件操作类详解
    Html中Label标记的作用和使用介绍
    正则表达式的威力轻松消除HTML代码
    只需一行代码就能让IE 6崩溃
    码农干货系列【17】Wind.js与Promise.js
    码农干货系列【3】割绳子(cut the rope)制作点滴:旋转(rotation)
    HTML5 Canvas开发者和读者的福音
    码农干货系列【8】世界上最简单的3D渲染(no webgl)
    码农干货系列【18】getting started with Promise.js(总)
    ProgressForm
  • 原文地址:https://www.cnblogs.com/chris-jichen/p/10072016.html
Copyright © 2011-2022 走看看