numbers

JavaScript - cannot add 2 numbers correctly

爱⌒轻易说出口 提交于 2020-01-05 08:30:26
问题 I am simulating a calculator using Javascript. Where a user can input 2 numbers in a given text box, and it will display sum, product, difference and division. Here is my function- function calculate(num1, num2) { console.log("First Number = " + num1 + " Second Number = " + num2); console.log("Sum = " + (num1 + num2)); console.log("Product =" + (num1 * num2)); console.log("Difference = " + (num1 - num2)); console.log("Division = " + (num1 / num2)); } When user inputs 1st number 4 and second

Java Regex: Extracting a Version Number

故事扮演 提交于 2020-01-05 08:15:53
问题 I have a program that stores the version number in a text file on the file system. I import the file within java and I am wanting to extract the version number. I'm not very good with regex so I am hoping someone can help. The text file looks like such: 0=2.2.5 BUILD (tons of other junk here) I am wanting to extract 2.2.5 . Nothing else. Can someone help me with the regex for this? 回答1: If you know the structure, you don't need a regex: String line = "0=2.2.5 BUILD (tons of other junk here)";

Fastest way to clamp a real (fixed/floating point) value?

强颜欢笑 提交于 2020-01-05 08:06:49
问题 Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be handled in separate functions. Obviously, I can do something like: double clampedA; double a = calculate(); clampedA = a > MY_MAX ? MY_MAX : a; clampedA = a < MY_MIN ? MY_MIN : a; or double a = calculate(); double clampedA = a; if(clampedA > MY_MAX

how can I check the input is only number and white space in regular expression?

夙愿已清 提交于 2020-01-05 04:32:05
问题 I have "222 22 222", "333 33 33 333", "1234/34", and "ab345 543" and I want to check whether these inputs are numeric and white space. I.E this case, the first and the second inputs should return True by using method Test of Regular Expression, or return its own value by using Exec method. The third and the fourth should return false. How could I do so in Regular Expression? Please help. Thank you. 回答1: You can test with this regular expression: /^[\d\s]+$/ Rubular If you want to also check

Removing lines with consecutive pairs

帅比萌擦擦* 提交于 2020-01-05 03:46:08
问题 I have a text file with a lot of number combinations. It looks like this: 1 2 3 4 5 6 1 2 3 4 5 7 1 2 3 4 5 8 1 2 3 4 5 9 1 2 3 4 5 10 Every line has 6 numbers with a space between each number. Numbers go from 1 to 37. I need an AWK command to remove any line with 2 or 3 consecutive pairs. For example: 1 2 6 9 13 14 4 5 18 19 25 26 Thanks! 回答1: awk '{pairs = 0; for (i = 1; i < NF; i++) if ($i + 1 == $(i + 1)) pairs++; if (pairs != 2 && pairs != 3) print}' input_file 来源: https://stackoverflow

How can I generate a random number from a group of numbers (not a range)

╄→гoц情女王★ 提交于 2020-01-05 02:52:13
问题 I would like to be able to generate a random number from a group of numbers, like for example: 1, 2, 3, 12, 11, 23, 45, 54, 10, 10, 12, 23, 35, 24. Using the rand and the srand i can generate random numbers different each time but i would like to able to condition the outcomes by generating random numbers from a group. 回答1: Example code Take an Array int a[]={ 1, 2, 3, 12, 11, 23, 45, 54, 10, 10, 12, 23, 35, 24}; int i = rand()%14; // here 14 is number of element in array. i will receive a

PHP calculating with negative numbers gives wrong values

北城余情 提交于 2020-01-05 02:39:09
问题 My PHP code: echo -100.35+100.00; echo '<br/>'; echo -478.35+478.32+0.03; Gives output: -0.34999999999999 -2.9559688030645E-14 I don't understand why, i tried (float) in front of the values but no difference. 回答1: You should use BC Math Functions for such calculations. 回答2: Take a look at this manual, it explains that the internal representation is not 100% exact for floats... it has to do with the internal representation of the floats I suspect. The manual does also refer to these methods

convert hexadecimal number string to double-precision number in java

☆樱花仙子☆ 提交于 2020-01-04 09:11:29
问题 how can we convert hexadecimal number string to double-precision number in java ? in matlab it's simple : >> hex2num('c0399999a0000000') ans = -25.6000 but could I do the same things in java also ? I tried parseInt() but this number is not integer. 回答1: I think you want Double.longBitsToDouble, like this: public class Test { public static void main(String[] args) { String hex = "c0399999a0000000"; long longHex = parseUnsignedHex(hex); double d = Double.longBitsToDouble(longHex); System.out

What is the fastest way to represent number as the sum of powers of two in python

点点圈 提交于 2020-01-04 07:49:43
问题 For example >>> two_powers(42) >>> (2, 8, 32) My current naive implementation (taken from here) looks like that def two_powers(num): return tuple(2 ** i for i, j in enumerate(bin(num)[-1: 1: -1]) if j == '1') But I hope there're faster ways to do this. 回答1: Try this: def two_powers(num): powers = [] while num != 0: powers.append(num & -num) num = num & (num - 1) return powers 回答2: Your solution is good actually, with a slight (big!) detail of efficiency: Use 1<<i (bitwise shift) instead of 2*

Calculations with Real Numbers, Verilog HDL

懵懂的女人 提交于 2020-01-04 06:33:52
问题 I noticed that Verilog rounds my real number results into integer results. For example when I look at simulator, it shows the result of 17/2 as 9. What should I do? Is there anyway to define something like a: output real reg [11:0] output_value ? Or is it something that has to be done by simulator settings? Simulation only (no synthesis). Example: x defined as a signed input and output_value defined as output reg. output_value = ((x >>> 1) + x) + 5; If x=+1 then output value has to be: 13/2=6