I have a color picker, and the hue values returned are numbers between zero and 1.
To create variations on the hue, I add .2 for 20%, .8 for 80% etc.
How can I keep the numbers going around the circle, so that when the number goes above 1, it subtracts the 1. How can I make this number into this number:
1.73540012511 ---> .7354
I tried using a "cents" javascript, (http://www.irt.org/script/6.htm) but this returned values greater than 1:
http://jsfiddle.net/Se9Dn/
I can't use Math.min, because I want 1.2 to become .2 (not to have the colors all become red when they hit 1).
Thanks. :)
Edit:
Confirming solution, with rounding added. Assuming you have an HSL color (Hue, Saturation, Luminosity) returned from Farbtastic and want to adjust Hue mathematically:
hslcolor = (.73802938, .59832908, .948987);
colorStep = .2;
newcolor[0] = Math.round ( ((1*colorStep +hslcolor[0])%1)*10000 ) /10000;
Watch out those parenthesis are very tricky in there.
You can use the modulo operator % (MDN docu).
So for adding, e.g., 0.8:
result = (result + 0.8) % 1;
EDIT
The modulo operator of JavaScript is actually more of a remainder operator.
Citing the ECMAScript spec Section 5.2:
The notation “x modulo y” (y must be finite and nonzero) computes a value k of the same sign as y (or zero) such that abs(k) < abs(y) and x−k = q × y for some integer q.
You just subtract the integer:
function cent(amount) { return (+amount) - Math.floor(+amount); }
If the number can be negative, then the it depends on the semantics you want. I'll leave that as an exercise :-)
Related
I know if I round up -1.5, it's -2.
so I tried to do with C# it returns -2 correctly.
Console.WriteLine(Math.Round(-1.5, 0));
also I tried to do with Excel, it also returns -2.
=Round(-1.5,0)
but when I do with javascript, it returns -1.
Math.round(-1.5)
why this values are different?
and how can I get -2 instead of -1 when I do this with javascript?
Math.round(Math.abs(-1.5));
your value is negative that's why it gets -1. Just get the absolute value and then round it and multiply it to -1 to get -2.
yes round in javascript works as you said. One solution is convert your negative number to positive then use Math.round. At last you should convert your number to negative number.
function myFunction() {
num = -1.5;
document.getElementById("demo").innerHTML = Math.round(num);
if(num < 0)
document.getElementById("demo").innerHTML = -1 * Math.round(Math.abs(num));
}
That's just how they made it. It is acknowledged that it is different than most languages.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value. If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. Note that this differs from many languages' round() functions, which often round this case to the next integer away from zero, instead giving a different result in the case of negative numbers with a fractional part of exactly 0.5.
I have objects in array:
x[{a=2.99, b=5.11}{a=4.99, b=2.11}]
And I want it to display 1 decimal with Math round, as I use Math.round(x[0].a*10)/10; it displays 3, while it works fine if I use just numbers as Math.round(2.99*10)/10.
Why is that?
please try the below code
x[0].a.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
you can round off to 1 by changing regex patter from {0, 2} to {0, 1}
Thiss might work -- Math.floor() and toFixed(1) are the keys here... rounding will cause the original input to change to the nearest integer.
function roundToTenth(inputVal) {
return Math.floor(inputVal * 10) * 0.1;
}
console.log(roundToTenth(2.99).toFixed(1));
Division is slower than multiplication is generally - and definitely using Regular Expression Matching is going to be slower than multiplication is....
All I'm doing in the code above is saying "Take the number times 10 and then turn it into a straight Integer with no Decimals,
so
2.99 * 10 = 29.9 which then becomes 29
finally, since I use the operation * 0.1 it becomes a float like 2.900000004 and I use toFixed(1) to strip out all those pesky 0s at the end
I saw this answer on how to convert a negative number to positive, but have a slightly different situation: I’m doing some coding in Apache Cordova and getting accelerometer data I need to flip.
So when the accelerometer returns an X axis value of -5 I need to convert it to 5 and the opposite as well; if the X axis value is 5 the new X axis value should be -5.
I understand how to do -Math.abs() and such, but how can I accommodate a situation like this in my code?
You can do a simple math at this context, no need of Math.abs,
x_value = x_value * -1;
Or you can negate the value like,
x_value = -(x_value);
While negating, there is a chance to get -0, But we don't need to worry about it, since -0 == 0. Abstract equality comparison algorithm is telling so in Step 1 - c - vi.
You can multiply any number by -1 to get its opposite.
Example:
5 * -1 = -5
-5 * -1 = 5
Why do you need Math.abs?
x_value = -1 * x_value;
works for every scenario I can envision.
I suggest to use the multiplication assignment with -1.
The multiplication assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable. See the multiplication operator for more details.
value *= -1;
Well, my solution is to create a ternary like this to see if the x_value is greater than 0 or not and act accordingly:
x_value = x_value > 0 ? -Math.abs(x_value) : Math.abs(x_value);
I am in a situation where a JavaScript function produces numbers, such as 2.5. I want to have these point five numbers rounded down to 2, rather than the result of Math.round, which will always round up in such cases (ignoring the even odd rule), producing 2. Is there any more elegant way of doing this than subtracting 0.01 from the number before rounding? Thanks.
Just negate the input and the output to Math.round:
var result = -Math.round(-num);
In more detail: JavaScript's Math.round has the unusual property that it rounds halfway cases towards positive infinity, regardless of whether they're positive or negative. So for example 2.5 will round to 3.0, but -2.5 will round to -2.0. This is an uncommon rounding mode: it's much more common to round halfway cases either away from zero (so -2.5 would round to -3.0), or to the nearest even integer.
However, it does have the nice property that it's trivial to adapt it to round halfway cases towards negative infinity instead: if that's what you want, then all you have to do is negate both the input and the output:
Example:
function RoundHalfDown(num) {
return -Math.round(-num);
}
document.write("1.5 rounds to ", RoundHalfDown(1.5), "<br>");
document.write("2.5 rounds to ", RoundHalfDown(2.5), "<br>");
document.write("2.4 rounds to ", RoundHalfDown(2.4), "<br>");
document.write("2.6 rounds to ", RoundHalfDown(2.6), "<br>");
document.write("-2.5 rounds to ", RoundHalfDown(-2.5), "<br>");
do this:
var result = (num - Math.Floor(num)) > 0.5 ? Math.Round(num):Math.Floor(num);
Another way exists that is to use real numbers (instead of 0.2 use 20, 0.02 use 2, etc.), then add floatingPoints variable that will divide the result (in your case it's 2). As a result you can operate as Number/(10^floatingPoints).
This solution is wide across Forex companies.
You can also use this function to round with no decimal part and .5 down rule (Only positive numbers):
function customRound(number) {
var decimalPart = number % 1;
if (decimalPart === 0.5)
return number - decimalPart;
else
return Math.round(number);
}
And sorry for my english.
So I ran across a small piece of code that looks like this
Math.random() * 5 | 0 and was confused by what it did.
after some inspecting, it seems like the comparison turns the decimal into an integer. is that right? and so the piece of code is another way is saying give me a random number between 0 and 4. Can anyone explain why that is?
1) Math.random() function always return decimal value and will be less than one. Ex - 0.2131313
random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
2) Math.random()*5 will always be less than 5. (maxvalue - 4.99999).
3) The bitwise operator '|' will truncate the decimal values.
Edit : Paul is correct. '|' does more than just truncate.
But in this case Math.random()*5|0 - It truncates the decimal and returns the integar.