标签: arithmetic-expressions

C拼图;乘以 Char

最近我的 C 教授给了我们以下难题:

char c1, c2, c3;

c1 = 'a';
c2 = 'e';
c3 = c1 * c2;
printf("%c",c3);
Run Code Online (Sandbox Code Playgroud)

答案:E

然而,除了已经记住产品之外,我有点困惑如何直观地解决这个问题。根据我的研究,字符“a”和“b”的 int 值分别为 97 和 101,“E”为 69,这就是我很难理解该结果是如何实现的。

c arithmetic-expressions char

1
推荐指数
1
解决办法
1922
查看次数

简写算术运算符在Scala中返回不同的结果 - EG:3 + 2!= 3. +(2)

可能重复:
Scala运算符奇怪

我对Scala很新,我读到用这种语言,一切都是对象,很酷.另外,如果一个方法只有一个参数,那么我们可以省略'.' 和parentesis'()',没关系.

所以,如果我们采用以下Scala示例:3 + 2'3'和'2'是两个Int对象而'+'是一个方法,听起来不错.然后3 + 2只是简写3.+(2),这看起来很奇怪,但我仍然得到它.

现在让我们比较下面的代码块REPL:

scala> 3 + 2
res0: Int = 5

scala> 3.+(2)
res1: Double = 5.0
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?为什么显式语法返回一段Double时间,速记返回Int

scala arithmetic-expressions operators

0
推荐指数
1
解决办法
218
查看次数

算术运算有空吗?

我知道以下,如果可能的话,将是一个绝对不好的做法,但我想知道这是否可行.

问题如下:是否有可能在C++中(并且在某种程度上编译器不会抛出任何警告),使用返回void的函数执行无用的算术运算.

std::vector<int> v;
int i = 42 + v.resize(42); 
/* How to transform the last line to execute resize and to have i = 42 */
Run Code Online (Sandbox Code Playgroud)

我知道这是愚蠢的,但这不是问题......

c++ arithmetic-expressions void c++11

0
推荐指数
2
解决办法
149
查看次数

如何在Java中显示NaN?

在Java中,我除以零,这显然不会产生数字.

public class tempmain {
    public static void main(String args[])
    {
        float a=3/0;
        System.out.println(a);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望输出为"NaN",但它显示以下错误:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at tempmain.main(tempmain.java:6)
Run Code Online (Sandbox Code Playgroud)

是否有任何方式(除此之外if(denominator==0))可以显示NaN?

java math arithmetic-expressions nan

0
推荐指数
1
解决办法
919
查看次数

算术在Python中被破坏了吗?

当我进行这个计算时, 2*(5+5/(3+3))*3 我在Python中得到30(2.7).但它似乎2*(5+5/(3+3))*3是等于35.谁能告诉我为什么python给了我30而不是35的答案?我已经使用JavaScript,Lua和Mac计算器进行了测试,他们向我展示了35.

为什么Python计算错误?

http://ideone.com/yiFJxS

python math arithmetic-expressions python-2.7

0
推荐指数
1
解决办法
85
查看次数

如何找到六位数的倒数第二位?

例如,如果数字是 431678,我需要找到 7 和 8。

要找到 8,我将数字(带 %)除以 10。我不知道如何找到 7。我假设我可能使用除法以 10000?如何让我的程序打印 7(倒数第二个数字)?这显然是一个简单的程序,所以我正在寻找简单的算术。

c++ arithmetic-expressions

0
推荐指数
1
解决办法
6190
查看次数

double、long double、float 和 float128 的比较?

为什么 double、long double 或 float 之间没有区别?

例如,piin的计算C++是,

#include <iostream>
#include <quadmath.h>
#include <math.h> 

int main()
{
  float  PI0 = acos(-1.0);
  double PI1 = acos(-1.0);
  long double PI2= acos(-1.0L);

  __float128 x = acos(-1.0);
  char buf[128];
  quadmath_snprintf (buf, sizeof buf, "%*.34Qf",10, x);


  std::cout << "Float=" << PI0 << std::endl;
  std::cout << "Double=" << PI1 << std::endl;
  std::cout << "LongDouble=" << PI2 << std::endl;
  std::cout << "Float128=" << buf << std::endl;

}
Run Code Online (Sandbox Code Playgroud)

在终端:

g++ -std=c++14 FileName.cpp -Wall -pedantic -lquadmath …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point arithmetic-expressions

0
推荐指数
1
解决办法
336
查看次数

Yii2:如何在简单的find()响应中添加数学?

我有一个使用Yii2 find()函数创建的数组(称为产品),有两个字段(称为名称价格),我需要做一个简单的算术加法:price + 5.但我不知道该怎么做.

使用SQL很容易但是使用Yii2我不知道如何解决它.

$products = Products::find()
    ->select([
        'name',
        'price',
        'price' + 5
    ])
    ->asArray()
    ->all();
Run Code Online (Sandbox Code Playgroud)

php math arithmetic-expressions yii2

0
推荐指数
1
解决办法
116
查看次数

C 中被误解的运算符优先级

我正在做一些关于C语言中操作执行顺序的练习,我遇到了一个我不太理解的情况。

int d = 1;
int e = d--/10;     // the result of e will be 0;
Run Code Online (Sandbox Code Playgroud)

在计算“e”的值之前,我们先递减“d”,然后再进行除法。

另一方面,在“f”中,我们在递减“d”之前进行了除法!

int d = 1;
int f = 10/d--;     // the result of f will be: 10
Run Code Online (Sandbox Code Playgroud)

我的问题是:既然知道在这两种情况下“d”的减量都是后减量,为什么使用的“d”值存在差异?

c arithmetic-expressions operation operator-precedence

0
推荐指数
1
解决办法
90
查看次数

哪个错了?Java,数学还是我......?困惑...!

我有以下SOP:

System.out.println(9 - ((9 / 2) * 2));
Run Code Online (Sandbox Code Playgroud)

从我在学校学到的数学,它应该评估0.

但我得到的输出是1...... !!!

我已经尝试在输出中评估Google中的相同表达式0.

根据数学,Java评估为1,它被评估为0.任何解释?

java expression arithmetic-expressions

-1
推荐指数
1
解决办法
135
查看次数