arithmetic-expressions

64 bit mathematical operations without any loss of data or precision

蹲街弑〆低调 提交于 2020-01-06 04:07:25
问题 I believe there isn't any portable standard data type for 128 bits of data. So, my question is about how efficiently 64 bit operations can be carried out without loss of data using existing standard data-types. For example : I have following two uint64_t type variables: uint64_t x = -1; uint64_t y = -1; Now, how the result of mathematical operations such as x+y, x-y, x*y and x/y can be stored/retrieved/printed ? For above variables, x+y results in value of -1 which is actually a

Arithmetic expression in swi-prolog

天涯浪子 提交于 2020-01-05 21:13:52
问题 I have the following prolog program: set_1(2). p(X) :- set_1(X+1). I'm using SWI-Prolog version 5.10.4 for i386 to run the query p(1) on this program. The answer is 'false'. I expect the answer to be 'true', because set_1(X+1) should be grounded as set_1(2) and resolved with the first fact. Why the answer is false and how can I get 'true' ? 回答1: If you want X+1 to unify with 2 in your example, you'll need to code this using is/2 . By itself X+1 is a valid Prolog term, but even when X is

Python floating point arithemetic basics

一世执手 提交于 2020-01-04 13:35:34
问题 As expected because of its finite precision, Python's floating point multiplication is not distributive over addition: In [10]: 200 * 0.1 + 200 * 0.2 Out[10]: 60.0 In [11]: 200 * (0.1 + 0.2) Out[11]: 60.00000000000001 And addition is not associative: In [12]: 1e14 + (48.18 + 18.26) Out[12]: 100000000000066.44 In [13]: (1e14 + 48.18) + 18.26 Out[13]: 100000000000066.45 But is addition commutative? multiplication? 回答1: Yes, addition and multiplication are commutative (with the exception of when

Are basic arithmetic operations in C# atomic

随声附和 提交于 2019-12-31 00:43:14
问题 Are the basic arithmetic operations Thread safe? For example, if there is ++ operation on a global variable, which will be modified from different threads, is it necessary to a lock around it? For example void MyThread() // can have many running instances { aGlobal++; } or should it be void MyThread() { lock( lockerObj) { aGlobal++; } } 回答1: The spec sums it up very well. Section 5.5, "Atomicity of variable references": Reads and writes of the following data types are atomic: bool, char, byte

CPU Usage from Linux then using it in a arithmetic expression

人盡茶涼 提交于 2019-12-25 03:15:53
问题 I have this line in bash, cpu= `top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print $1}' Thats working as I want it if I echo it but when used in the next line as part of a equation. joulesFinal=$(($joules2*$cpu)) I get the error from system arithmetic expression: expecting EOF: "6*93.4" Help appreciated! 回答1: Bash arithmetic is integer only. It won't accept fractional numbers like 93.4. You need to pipe the expression through bc . joulesFinal=`echo $joules2 * $cpu |

Bash - arithmetic in array index

天涯浪子 提交于 2019-12-24 14:36:07
问题 Say I have an array arr and an index x . How do I assign something to the array at index x+1? I'm getting bugs by trying the following, if one of them is correct I'd love to know which one and if not what am I doing wrong? arr[$x+1]="hi" # Doesn't work arr[$((x+1))]="hi" # Nope 回答1: Almost there. arr[(($x+1))]="hi" 来源: https://stackoverflow.com/questions/20762751/bash-arithmetic-in-array-index

Regular expression for arithmetic expression

自闭症网瘾萝莉.ら 提交于 2019-12-24 03:34:56
问题 I,m trying to write a regex to check if the given string is like a + b, 2 + a + b, 3 + 6 * 9 + 6 * 5 + a * b, etc... Only + and * operators. I tried if (str.matches("(\\d|\\w \\+|\\*){1,} \\d|\\w")) Unfortunately it only handles cases like 3 * 7 ... (numeric * numeric). Waiting for your answers, thanks for reading me. 回答1: Put * and + inside a character class. str.matches("\\w(?:\\s[+*]\\s\\w)+"); DEMO 回答2: This will handle cases of simple and chained calculations [0-9A-Za-a]*( ){0,}([+-/*](

How to do arithmetic expression evaluation in prolog?

£可爱£侵袭症+ 提交于 2019-12-23 03:46:14
问题 I am trying to solve an arithmetic expression in prolog (implementation - eclipse prolog). The arithmetic expression to be solved is like this: A * (C + B * X) + D * X = E X is the value to be computed, and all others (A,B,C,D,E) are all numbers. For example: 5 * (3 + 2*X) + 2*X = 39, on computing should assign X with the value 2. The query(goal) that would be entered into Prolog would take the form: ?- compute( 5*(3+2*X)+2*X = 39, Result). The 'Result' and value of 'X' should be tied

Java Scientific Calculator Regular Expression

跟風遠走 提交于 2019-12-23 03:17:21
问题 What will be the regular expression in java for like this expression (3+2)+23/12-(43/54) in which left parentheses is create than user will be able to put right one and if left parentheses is not created than user will not able to put right parentheses .And if left parentheses is created 3 time than user will be able to just put right parentheses 3 times only to close the expression which is open by left parentheses. Thanks 回答1: In a nutshell, this is not possible using standard regular

Expression recursion level exceeded

匆匆过客 提交于 2019-12-22 09:03:21
问题 Don't know why there is error in the following examples: $ a=1; (( a > 0 )) && echo y || echo n y $ a=x; (( a > 0 )) && echo y || echo n n $ a=a; (( a > 0 )) && echo y || echo n -bash: ((: a: expression recursion level exceeded (error token is "a") n 回答1: $ a=a ( no error ) $ declare -i a $ a=a -bash: ((: a: expression recursion level exceeded (error token is "a") This behavior is because declare -i puts the RHS of an assignment in arithmetic context. In arithmetic context, bash dereferences