Questions tagged [arithmetic-expressions]

An arithmetic expression is an expression that results in a numeric value. There are two kinds of numeric values, integers (whole numbers), and real or floating point numbers (numbers containing a decimal point).

677 questions
2
votes
1 answer

Python floating point arithemetic basics

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…
xnx
  • 24,509
  • 11
  • 70
  • 109
2
votes
0 answers

Unexpected output in R with modulus operator

((10*(7655.7-7652.3))%/%(2)) [1] 16 ((10*(655.7-652.3))%/%(2)) [1] 17 ((10*(7655.7-7652.3))%/%(2)) [1] 16 ((10*(8655.7-8652.3))%/%(2)) [1] 17 ((10*(9655.7-9652.3))%/%(2)) [1] 17 ((10*(7655.7-7652.3))%/%(2)) [1] 16 %/% operator gives the…
2
votes
1 answer

Javascript: /e modifier for RegExp in Javascript

Is there something like Perls /e modifier in JavaScript to integrate simple arithmetic like multiplication and addition to a regular expression that is evaluated with the RegExp object? I want to transfer a pattern for URL generation which may…
Sebastian Barth
  • 4,079
  • 7
  • 40
  • 59
2
votes
2 answers

How does the integer overflow work in C?

I am a bit confused about how is arithmetic calculations are handled in a fixed point environment. Consider following lines of code: /* unsigned short is 16 bit.*/ unsigned short x = 1000; unsigned short res; /* Case1: The following yields correct…
sdevikar
  • 437
  • 6
  • 20
2
votes
1 answer

Syntax error: invalid arithmetic operator (error token is ".")

I have to search word occurrences in a plain text file using a script. The script that I wrote is: #!/bin/bash if [ $# -ne 1 ] then echo "inserire il file che si vuole analizzare: " read fileIn=$REPLY else …
shogitai
  • 1,823
  • 1
  • 23
  • 50
2
votes
1 answer

DRY arithmetic expression evaluation in Prolog

I wanted to write evaluating predicate in Prolog for arithmetics and I found this: eval(A+B,CV):-eval(A,AV),eval(B,BV),CV is AV+BV. eval(A-B,CV):-eval(A,AV),eval(B,BV),CV is AV-BV. eval(A*B,CV):-eval(A,AV),eval(B,BV),CV is…
Legat
  • 1,379
  • 3
  • 11
  • 20
2
votes
4 answers

Convert an alphabetic string into digits and recursively sum them so they're below a certain max

Here's an arithmetical puzzler for you StackOverflowers. I'm working on a JS game that'll allow someone to enter their name and will generate another name based on what they enter. Sort of like this. Basically every time someone enters a particular…
And Finally
  • 5,602
  • 14
  • 70
  • 110
2
votes
3 answers

Java very small number arithmetic operation

The result of expression: 2.227E-19-1.0+1.0 would be 0.0 How can I get result of 2.227E-19 ? I have an expression a-b+1 and a always be a very small number. b might be close to 1.0 HOW to keep accuracy when a very small number operate with a…
x y
  • 21
  • 2
2
votes
2 answers

Is there an easy way to get the POSIX shell to interpret numbers with leading zeroes as decimal?

In a POSIX shell script, I'd like to use decimal numbers generated by seq -w both in string manipulations and in simple calculations using $(( ... )). This breaks because numbers with leading zeroes are interpreted as octal numbers. I have come up…
hillu
  • 9,423
  • 4
  • 26
  • 30
2
votes
2 answers

Solving simple string expressions (1+2*3) in Java [Almost Done]

I'm trying to solve simple string expressions e.g. 1+2*3/4, without brackets. I'm done with simple integer part, the above expression will work perfectly, but now i'm stuck with decimal values, for example 1.1/2.2*4.4 All i want is to push the whole…
Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
2
votes
1 answer

Bash - arithmetic in array index

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" #…
Dori
  • 1,035
  • 1
  • 12
  • 22
2
votes
2 answers

Dot product Prolog/3 need Hint for the SUM

Good Day I am doing the problema of arithmetic in prolog and Yes its the dot Product I have Searched and found a mess of code that did not equal to what the book is asking me. Its a /3 so this is what i have so far but i need to sum the result of…
HTLINK
  • 65
  • 1
  • 7
2
votes
4 answers

Number Base in Bash

To force numbers to be interpreted in base10, you can prefix with 10#. Specifically 10#08 and 10#09 will be interpreted as valid decimal numbers, and not invalid octal numbers. (I'm taking the output of date +%S) However, it seems I then can't use…
rojomoke
  • 3,765
  • 2
  • 21
  • 30
2
votes
9 answers

Unboxing Long in java

In some code I see this: private void compute(Long a, Long b, Long c) { long result = a-(b+c); ... It seems a bit strange that the result is stored in a primitive long instead of a Long object corresponding to its operands. Are there any…
u123
  • 15,603
  • 58
  • 186
  • 303
2
votes
6 answers

Little endian or Big endian

#include union Endian { int i; char c[sizeof(int)]; }; int main(int argc, char *argv[]) { union Endian e; e.i = 1; printf("%d \n",&e.i); printf("%d,%d,\n",e.c[0],&(e.c[0])); …
tez
  • 4,990
  • 12
  • 47
  • 67