首页 > 编程知识 正文

包含jsmath源码的词条

时间:2023-12-12 16:20:09 阅读:314914 作者:QLGX

本文目录一览:

js中的math对象有哪些常用的方法,其用法和作用是什么

abs(x)

返回数的绝对值。

acos(x)

返回数的反余弦值。

asin(x)

返回数的反正弦值。

atan(x)

以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。

atan2(y,x)

返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。

ceil(x)

对数进行上舍入。

cos(x)

返回数的余弦。

exp(x)

返回 e 的指数。

floor(x)

对数进行下舍入。

log(x)

返回数的自然对数(底为e)。

max(x,y)

返回 x 和 y 中的最高值。

min(x,y)

返回 x 和 y 中的最低值。

pow(x,y)

返回 x 的 y 次幂。

random()

返回 0 ~ 1 之间的随机数。

round(x)

把数四舍五入为最接近的整数。

sin(x)

返回数的正弦。

sqrt(x)

返回数的平方根。

tan(x)

返回角的正切。

toSource()

返回该对象的源代码。

valueOf()

返回 Math 对象的原始值。

作用和用法?数学函数需要什么用什么啊。

number-precision 实现js高精度运算 源码

type numType = number | string;

/**

* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。

* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998

*/

/**

* 把错误的数据转正

* strip(0.09999999999999998)=0.1

*/

function strip(num: numType, precision = 15): number {

  return +parseFloat(Number(num).toPrecision(precision));

}

/**

* Return digits length of a number

* @param {*number} num Input number

*/

function digitLength(num: numType): number {

  // Get digit length of e

  const eSplit = num.toString().split(/[eE]/);

  const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);

  return len 0 ? len : 0;

}

/**

* 把小数转成整数,支持科学计数法。如果是小数则放大成整数

* @param {*number} num 输入数

*/

function float2Fixed(num: numType): number {

  if (num.toString().indexOf('e') === -1) {

    return Number(num.toString().replace('.', ''));

  }

  const dLen = digitLength(num);

  return dLen 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);

}

/**

* 检测数字是否越界,如果越界给出提示

* @param {*number} num 输入数

*/

function checkBoundary(num: number) {

  if (_boundaryCheckingState) {

    if (num Number.MAX_SAFE_INTEGER || num Number.MIN_SAFE_INTEGER) {

      console.warn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);

    }

  }

}

/**

* 精确乘法

*/

function times(num1: numType, num2: numType, ...others: numType[]): number {

  if (others.length 0) {

    return times(times(num1, num2), others[0], ...others.slice(1));

  }

  const num1Changed = float2Fixed(num1);

  const num2Changed = float2Fixed(num2);

  const baseNum = digitLength(num1) + digitLength(num2);

  const leftValue = num1Changed * num2Changed;

  checkBoundary(leftValue);

  return leftValue / Math.pow(10, baseNum);

}

/**

* 精确加法

*/

function plus(num1: numType, num2: numType, ...others: numType[]): number {

  if (others.length 0) {

    return plus(plus(num1, num2), others[0], ...others.slice(1));

  }

  const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));

  return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;

}

/**

* 精确减法

*/

function minus(num1: numType, num2: numType, ...others: numType[]): number {

  if (others.length 0) {

    return minus(minus(num1, num2), others[0], ...others.slice(1));

  }

  const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));

  return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;

}

/**

* 精确除法

*/

function divide(num1: numType, num2: numType, ...others: numType[]): number {

  if (others.length 0) {

    return divide(divide(num1, num2), others[0], ...others.slice(1));

  }

  const num1Changed = float2Fixed(num1);

  const num2Changed = float2Fixed(num2);

  checkBoundary(num1Changed);

  checkBoundary(num2Changed);

  // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正

  return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));

}

/**

* 四舍五入

*/

function round(num: numType, ratio: number): number {

  const base = Math.pow(10, ratio);

  return divide(Math.round(times(num, base)), base);

}

let _boundaryCheckingState = true;

/**

* 是否进行边界检查,默认开启

* @param flag 标记开关,true 为开启,false 为关闭,默认为 true

*/

// 这里可以设置边界检查(默认是true)

function enableBoundaryChecking(flag = true) {

  _boundaryCheckingState = flag;

}

// 输出上面的方法

export { strip, plus, minus, times, divide, round, digitLength, float2Fixed, enableBoundaryChecking };

export default {

  strip,

  plus,

  minus,

  times,

  divide,

  round,

  digitLength,

  float2Fixed,

  enableBoundaryChecking,

};

JavaScript Math.random()问题

那是因为javascript把min_num作为了默认字符串处理了。

只需要把var num=Math.random()*(max_num-min_num)+min_num;改一下就可以了

var num=Math.random()*(max_num-min_num)+min_num*1;

强制把min_num作为数字处理

JS中Math[this

在javascript中对象获取属性的方式有两种,一种是使用运算符“.”跟上属性名,一种是使用['属性名']的方式。比如:

window.location 与window['location']都可以访问到window对象的location属性。

点运算符(成员运算符)使用方便,但是在需要动态获取对象属性时,也就是说,我要获取的属性它的名称本身也是通过传参确定时,就需要使用方括号的方式,你现在就属于这种情况。如果你有过反射方面的编程经验就不难理解了。

js Math 对象的方法

1.丢弃小数部分,保留整数部分

parseInt(5/2)

2.向上取整,有小数就整数部分加1

Math.ceil(5/2)

3,四舍五入.

Math.round(5/2)

4,向下取整

Math.floor(5/2)

Math

对象方法

FF:

Firefox,

IE:

Internet

Explorer

方法

描述

FF

IE

abs(x)

返回数的绝对值。

1

3

acos(x)

返回数的反余弦值。

1

3

asin(x)

返回数的反正弦值。

1

3

atan(x)

以介于

-PI/2

PI/2

弧度之间的数值来返回

x

的反正切值。

1

3

atan2(y,x)

返回从

x

轴到点

(x,y)

的角度(介于

-PI/2

PI/2

弧度之间)。

1

3

ceil(x)

对数进行上舍入。

1

3

cos(x)

返回数的余弦。

1

3

exp(x)

返回

e

的指数。

1

3

floor(x)

对数进行下舍入。

1

3

log(x)

返回数的自然对数(底为e)。

1

3

max(x,y)

返回

x

y

中的最高值。

1

3

min(x,y)

返回

x

y

中的最低值。

1

3

pow(x,y)

返回

x

y

次幂。

1

3

random()

返回

~

1

之间的随机数。

1

3

round(x)

把数四舍五入为最接近的整数。

1

3

sin(x)

返回数的正弦。

1

3

sqrt(x)

返回数的平方根。

1

3

tan(x)

返回角的正切。

1

3

toSource()

返回该对象的源代码。

1

-

valueOf()

返回

Math

对象的原始值。

1

4

js中Math的几个函数

console.log(Object.getOwnPropertyNames(Math).filter((function (name) {

            return typeof Math[name] === 'function';

        })).length); //有35个函数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。