arithmetic-expressions

How to perform element-wise arithmetic operations (e.g. add, subtract, multiply) of two equally shaped lists with arbitrary nestings

别等时光非礼了梦想. 提交于 2021-02-08 08:35:17
问题 I want to perform element-wise mathematical operations (e.g. sum, multiply..) on two Python lists containing numbers or multiple nested lists which may contain again numbers or lists and so on. The shapes of the two lists are equal when performing the operation. Furthermore, the result should be of the same shape as the two input lists. A shape may differ in: length , width (i.e. number of nestings), order (e.g. the lists start with a number followed by a nested list, but it may also be that

What precision are floating-point arithmetic operations done in?

倾然丶 夕夏残阳落幕 提交于 2021-02-07 06:29:06
问题 Consider two very simple multiplications below: double result1; long double result2; float var1=3.1; float var2=6.789; double var3=87.45; double var4=234.987; result1=var1*var2; result2=var3*var4; Are multiplications by default done in a higher precision than the operands? I mean in case of first multiplication is it done in double precision and in case of second one in x86 architecture is it done in 80-bit extended-precision or we should cast operands in expressions to the higher precision

Why does Bash with -e option exit when 'let' expression evaluates to 0? [duplicate]

廉价感情. 提交于 2021-02-05 06:49:06
问题 This question already has answers here : bash set -e and i=0;let i++ do not agree (3 answers) Closed 5 years ago . I am struggling to understand why the bash -e option exits this script. It happens only when the expression calculated gives 0 : #!/bin/bash set -ex table_year=( 1979 1982 1980 1993 1995 ) year=$1 let indice=year-1 real_year=${table_year[$indice]} echo OK $real_year Is is ok when: ./bash_test_array 2 but not when: ./bash_test_array 1 indice is this case equals to 0 . Why the -e

Python error in basic subtraction? [duplicate]

ⅰ亾dé卋堺 提交于 2021-01-17 06:59:49
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Python rounding error with float numbers python maths is wrong I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996. I explored a bit: sq = {} sub = {} for i in range(1000): sq[str(i/1000.)+'**2']=((i/1000.)**2) sub['1-'+str(i/1000.)]=(1.0-(i/1000.)) and discovered that this error happens with a somewhat random group of the

Python error in basic subtraction? [duplicate]

主宰稳场 提交于 2021-01-17 06:59:22
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Python rounding error with float numbers python maths is wrong I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996. I explored a bit: sq = {} sub = {} for i in range(1000): sq[str(i/1000.)+'**2']=((i/1000.)**2) sub['1-'+str(i/1000.)]=(1.0-(i/1000.)) and discovered that this error happens with a somewhat random group of the

Python error in basic subtraction? [duplicate]

你离开我真会死。 提交于 2021-01-17 06:56:56
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Python rounding error with float numbers python maths is wrong I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996. I explored a bit: sq = {} sub = {} for i in range(1000): sq[str(i/1000.)+'**2']=((i/1000.)**2) sub['1-'+str(i/1000.)]=(1.0-(i/1000.)) and discovered that this error happens with a somewhat random group of the

Python error in basic subtraction? [duplicate]

爱⌒轻易说出口 提交于 2021-01-17 06:56:51
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Python rounding error with float numbers python maths is wrong I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996. I explored a bit: sq = {} sub = {} for i in range(1000): sq[str(i/1000.)+'**2']=((i/1000.)**2) sub['1-'+str(i/1000.)]=(1.0-(i/1000.)) and discovered that this error happens with a somewhat random group of the

Python error in basic subtraction? [duplicate]

一个人想着一个人 提交于 2021-01-17 06:56:30
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Python rounding error with float numbers python maths is wrong I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996. I explored a bit: sq = {} sub = {} for i in range(1000): sq[str(i/1000.)+'**2']=((i/1000.)**2) sub['1-'+str(i/1000.)]=(1.0-(i/1000.)) and discovered that this error happens with a somewhat random group of the

Little endian or Big endian

旧城冷巷雨未停 提交于 2020-01-13 19:26:06
问题 #include <stdio.h> 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])); printf("%d,%d",e.c[sizeof(int)-1],&(e.c[sizeof(int)-1])); } OUTPUT: 1567599464 1,1567599464, 0,1567599467 LSB is stored in the lower address and MSB is stored in the higher address. Isn't this supposed to be big endian? But my system config shows it as a little endian architecture. 回答1: You system is definitely

How to multiply/divide/add/subtract numbers of different types?

好久不见. 提交于 2020-01-10 03:45:08
问题 I'm working through the second edition of the Rust handbook, and decided to try and make the classic Celsius-to-Fahrenheit converter: fn c_to_f(c: f32) -> f32 { return ( c * ( 9/5 ) ) + 32; } Compiling this with cargo build will yield the compile-time error: error[E0277]: the trait bound `f32: std::ops::Mul<{integer}>` is not satisfied --> src/main.rs:2:12 | 2 | return (c * (9 / 5)) + 32; | ^^^^^^^^^^^^^ the trait `std::ops::Mul<{integer}>` is not implemented for `f32` | = note: no