Skip to content
Advertisement

Tag: math

Math.floor VS Math.trunc JavaScript

Background I am making a function that receives a positive number and then rounds the number to the closest integer bellow it. I have been using Math.floor, but recently I discovered Math.trunc. I am aware that both will return the same value, given a positive number, and that they work in completely different ways. I am interested in exploring this

Simple easing function in javascript

I’m having trouble figuring out this simple math problem. I have spent over two hours reading through various related answers on SO and Google, but it seems my high school math knowledge is gone. On the page I have an element, that, once it passes a threshold, gets scaled down, the closer it gets to the edge of the containing

Lat/Long equation in Javascript

I am trying to find an equation for finding the lat/long point between two lat/long points in Javascript. It would work something like this. getMiddle(lat1, lng1, lat2, lng2) <= would return [lat3, lat3] halfway distance wise (going around the earth obviously). I found this: Link Date: 10/11/2001 at 11:41:08 From: Doctor Rick Subject: Re: Determining lat and long of a

Getting place values of a number w/ modulus?

I need to get the place value of a random number submitted by a user. This number can by anything from 0-1000000000000000 (zero to one trillion). I think this can be achieved by using the JavaScript modulus % operator. The problem, I don’t really know how to use it, nor do I understand it. Here is the Fiddle. (All I

JavaScript factorial prevent infinity

I have been using this function for calculating factorial numbers in JavaScript: All seemed to be going well until I tried the number 500. It returned infinity. Is there a way that I can prevent infinity as an answer? Thank you. Answer You indeed need to use bignumbers. With math.js you can do: This will output: 1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Javascript: Round up to the next multiple of 5

I need a utility function that takes in an integer value (ranging from 2 to 5 digits in length) that rounds up to the next multiple of 5 instead of the nearest multiple of 5. Here is what I got: When I run round5(32), it gives me 30, where I want 35. When I run round5(37), it gives me 35,

Javascript/jQuery – Display inches in feet and inches

I’m making a BMI calculator (see JSFiddle – http://jsfiddle.net/b5ww2/) and I want the height in inches to display in feet and inches. I also want the weight in pounds to be displayed in stones and pounds. This is the code I’m using to convert the slider value to cm and inches: My js knowledge isn’t great – especially when it

Advertisement