我在ideone.com上尝试了以下测试
(注意:我的本地机器不会出现此问题)
#include <stdio.h>
int main(void) {
float f = abs( 2 ); printf( "%f \n", f ); // 2.000000
float g = abs( 1.5 ); printf( "%f \n", g ); // 0.000000
return 0;
}
Run Code Online (Sandbox Code Playgroud)
abs的原型是'int abs(int)'
因此,我希望g返回1或2; 我希望'1.5'可以转换为整数.
这可以在ideone.com上进行演示:http://ideone.com/reyITY
有人可以解释这种行为吗?
PS我知道我应该使用fabs(或fabsf?)但是我想知道为什么这会破坏它的方式
我需要编写以下代码(在C++ 11中):
X x;
if( cond ) {
x = X(foo);
}
else {
x = X(foo, bar);
}
// ... continue using x
Run Code Online (Sandbox Code Playgroud)
问题是'X x'将调用默认构造函数并创建一个X类型的不需要的对象.
我怎么解决这个问题?我怀疑可能有一些涉及新的&&运算符的解决方案,但我看不到它......
我的 .yaml 看起来像这样:
apiVersion: batch/v1
kind: Job
metadata:
name: foo
namespace: bar
spec:
template:
spec:
imagePullSecrets:
- name: $DOCKERHUB_REGISTRY_SECRET
containers:
- name: django
image: $DOCKERHUB_USER/$DOCKERHUB_IMAGENAME:$VERSION
command: [ 'python manage.py makemigrations' ]
:
Run Code Online (Sandbox Code Playgroud)
不过,我看到了很多替代方案command:
command: [ 'python', 'manage.py', 'makemigrations' ]
Run Code Online (Sandbox Code Playgroud)
command: [ '/bin/sh -c', 'python', 'manage.py', 'makemigrations' ]
Run Code Online (Sandbox Code Playgroud)
command: [ '/bin/sh', '-c' ]
args: [ 'python', 'manage.py', 'makemigrations' ]
Run Code Online (Sandbox Code Playgroud)
command: [ 'python' ]
args: [ 'manage.py', 'makemigrations' ]
Run Code Online (Sandbox Code Playgroud)
ETC。
从美学上来说,我更喜欢最后一种方法,因为它清楚地将命令与参数分开。
但我想检查一下:这些版本之间有什么细微的区别吗?是否存在任何令人信服的逻辑来支持其中一个而不是其他?
我有以下 BASH 代码:
response=$( curl -Ls $endpoint )
if [ -n "$response" ]; then # nonempty
echo "$response" | jq .
fi
Run Code Online (Sandbox Code Playgroud)
问题是有时响应可以是非空的,但不是 JSON(如果它不是 200)。
jq如果输出是有效的 JSON,是否可以通过管道传输输出?
以下工作:
echo $x | jq . 2>/dev/null || echo $x
Run Code Online (Sandbox Code Playgroud)
测试:
> x='{"foo":123}'; echo $x | jq . 2>/dev/null || echo "Invalid: $x"
{
"foo": 123
}
> x='}'; echo $x | jq . 2>/dev/null || echo "Invalid: $x"
Invalid: }
Run Code Online (Sandbox Code Playgroud)
但是,我对此感到不舒服。
我bash在 MacOS 上使用 shell 感觉很舒服。但卡特琳娜将其替换为zsh.
为什么?我可以把它切换回来吗?
如果是这样,怎么办?
这样做有意义吗?有什么问题吗?
这是我的复数:我正在从文件中检索它.
re, im = line[11:13]
print( re ) # -4.04780617E-02
print( im ) # +4.09889424E-02
Run Code Online (Sandbox Code Playgroud)
目前它只是一对弦.如何将这些组合成一个复数?
我已经尝试了五次.
z = complex( re, im )
# ^ TypeError: complex() can't take second arg if first is a string
z = complex( float(re), float(im) )
# ^ ValueError: could not convert string to float: re(tot)
z = float(re) + float(im) * 1j
# ^ ValueError: could not convert string to float: re(tot)
z = complex( "(" + re + im + "j)" ) …Run Code Online (Sandbox Code Playgroud)