numbers

random number from -9 to 9 in C++

白昼怎懂夜的黑 提交于 2019-11-30 17:35:08
just wondering, if I have the following code: int randomNum = rand() % 18 + (-9); will this create a random number from -9 to 9? No, it won't. You're looking for: int randomNum = rand() % 19 + (-9); There are 19 distinct integers between -9 and +9 (including both), but rand() % 18 only gives 18 possibilities. This is why you need to use rand() % 19 . Your code returns number between (0-9 and 17-9) = (-9 and 8). For your information rand() % N; returns number between 0 and N-1 :) The right code is rand() % 19 + (-9); Do not forget the new C++11 pseudo-random functionality , could be an option

check NaN number

爱⌒轻易说出口 提交于 2019-11-30 17:08:01
Is it possible to check if a number is NaN or not? Yes, by use of the fact that a NaN is not equal to any other number, including itself. That makes sense when you think about what NaN means, the fact that you've created a value that isn't really within your power to represent with "normal" floating point values. So, if you create two numbers where you don't know what they are, you can hardly consider them equal. They may be but, given the rather large possibility of numbers that it may be (infinite in fact), the chances that two are the same number are vanishingly small :-) You can either

JavaScript Number.toLocaleString() with 4 digits after separator

杀马特。学长 韩版系。学妹 提交于 2019-11-30 16:47:38
Is there a way to get number.toLocaleString() with 4 digits after the comma? Example: var number = 49.9712; document.getElementById('id').innerText = number.toLocaleString(); Result: 49,9712 But now it always returns number with 2 digits after comma: 49,97 You may use second argument to provide some options. In your case, with default locale: number.toLocaleString(undefined, { minimumFractionDigits: 4 }) I found that var number = 49.9712; number.toLocaleString( { minimumFractionDigits: 4 }) gave the result of "49.971" In order to actually get the 4 decimal place digits, I did this: number

When I divide numbers in clojure I get a fraction , how do I get the decimal?

爷,独闯天下 提交于 2019-11-30 16:46:20
When I do (/ 411 125) , I don't get it in terms of decimal. How do I do that? user> (float (/ 411 125)) 3.288 user> (double (/ 411 125)) 3.288 user=> (clojure-version) "1.4.0" user=> (doc quot) ------------------------- clojure.core/quot ([num div]) quot[ient] of dividing numerator by denominator. nil user=> (quot 411 125) 3 As documented , integer division yields rational numbers. Try (/ 411.0 125) If you use a float for the dividend, you'll get a decimal answer. (/ 22.0 7) -> 3.142857142857143 There's also the (unchecked-remainder x y) function available. even this will work: (/ 22. 7) => 3

Objective-C: format numbers to ordinals: 1, 2, 3, .. to 1st, 2nd, 3rd

大兔子大兔子 提交于 2019-11-30 16:32:04
问题 In Objective C, is there any way to format an integer to ordinals 1 => "1st", 2 => "2nd" etc... that works for any language ? So if the user is French he will see "1er", "2ieme" etc.. Thanks a lot! Edit : This is for an iOs app 回答1: Have you taken a look at TTTOrdinalNumberFormatter which is in FormatterKit? It works great, and I'm pretty sure it's exactly what you're looking for. Here's an example taken from the kit: TTTOrdinalNumberFormatter *ordinalNumberFormatter = [

JavaScript generate random numbers without repeating

筅森魡賤 提交于 2019-11-30 16:24:28
问题 I have some code where I have an array of x amount of items. In this case, videos, and I want to randomly call a video, however if the current video already called is the same as the random number I want it to generate another random number until it's unique. Here's my code: var videoLinks = [ ['<iframe id="vid" src="https://www.youtube.com/embed/nYm2G4MnSkY?autoplay=1" frameborder="0" allowfullscreen></iframe>'], ['<iframe id="vid" src="https://www.youtube.com/embed/wAgZVLk6J4M?autoplay=1

JavaScript generate random numbers without repeating

大兔子大兔子 提交于 2019-11-30 16:15:09
I have some code where I have an array of x amount of items. In this case, videos, and I want to randomly call a video, however if the current video already called is the same as the random number I want it to generate another random number until it's unique. Here's my code: var videoLinks = [ ['<iframe id="vid" src="https://www.youtube.com/embed/nYm2G4MnSkY?autoplay=1" frameborder="0" allowfullscreen></iframe>'], ['<iframe id="vid" src="https://www.youtube.com/embed/wAgZVLk6J4M?autoplay=1&start=5&end=45" frameborder="0" allowfullscreen></iframe>'], ['<iframe id="vid" src="https://www.youtube

Rounding decimal numbers in perl, wrong result

浪子不回头ぞ 提交于 2019-11-30 16:10:11
问题 I hate decimal numbers. For 1.005 I don't get the result I expect with the following code. #!/usr/bin/perl -w use strict; use POSIX qw(floor); my $num = (1.005 * 100) + 0.5; print $num . "\n"; # 101 print floor($num) . "\n"; # 100 print int($num) . "\n"; # 100 For 2.005 and 3.005 it works fine. With this ugly "hack" I get the expected result. #!/usr/bin/perl -w use strict; use POSIX qw(floor); my $num = (1.005 * 100) + 0.5; $num = "$num"; print $num . "\n"; # 101 print floor($num) . "\n"; #

Rounding decimal numbers in perl, wrong result

被刻印的时光 ゝ 提交于 2019-11-30 16:03:06
I hate decimal numbers. For 1.005 I don't get the result I expect with the following code. #!/usr/bin/perl -w use strict; use POSIX qw(floor); my $num = (1.005 * 100) + 0.5; print $num . "\n"; # 101 print floor($num) . "\n"; # 100 print int($num) . "\n"; # 100 For 2.005 and 3.005 it works fine. With this ugly "hack" I get the expected result. #!/usr/bin/perl -w use strict; use POSIX qw(floor); my $num = (1.005 * 100) + 0.5; $num = "$num"; print $num . "\n"; # 101 print floor($num) . "\n"; # 101 print int($num) . "\n"; # 101 What is the correct way to do this? Orbling floor() is not for

Comparing anagrams using prime numbers

北战南征 提交于 2019-11-30 15:40:04
问题 There is the problem of trying to see if two unique strings are anagrams of each other. The first solution that I had considered would be to sort both strings and see if they were equal to each other. I have been considering another solution and I would like to discuss if the same would be feasible. The idea would be to assign a numerical value to each character and sum it up such that a unique set of characters would produce a unique value. As we are testing for anagrams, we do not mind if