IT

C에서 1,000,000,000을 1000 * 1000 * 1000으로 쓰는 이유는 무엇입니까?

lottoking 2020. 6. 26. 07:53
반응형

C에서 1,000,000,000을 1000 * 1000 * 1000으로 쓰는 이유는 무엇입니까?


Apple이 만든 코드에는 다음과 같은 줄이 있습니다.

CMTimeMakeWithSeconds( newDurationSeconds, 1000*1000*1000 )

표현 할 이유가 있습니까 1,000,000,000로는 1000*1000*1000?

1000^3그런 문제가 되지 않습니까?


곱셈 방식으로 상수를 선언하는 한 가지 이유는 가독성을 높이고 런타임 성능에는 영향을 미치지 않기 때문입니다. 또한 작가가 숫자에 대해 곱셈 방식으로 생각하고 있음을 나타냅니다.

이걸 고려하세요:

double memoryBytes = 1024 * 1024 * 1024;

다음보다 분명히 낫습니다.

double memoryBytes = 1073741824;

후자는 언뜻보기에 1024의 세 번째 힘을 보지 않습니다.

Amin Negm-Awad가 언급했듯이 ^연산자는 이진수 XOR입니다. 많은 언어에는 내장 된 컴파일 타임 지수 연산자가 없기 때문에 곱셈입니다.


왜 안돼 1000^3?

결과 1000^3는 1003 ^입니다. 비트 XOR 연산자입니다.

Q 자체를 다루지 않더라도 설명을 추가합니다. 질문자의 예 에서처럼 항상 평가 x^y하지는 않습니다x+y . 모든 비트를 조정해야합니다. 예제의 경우 :

1111101000₂ (1000₁₀)
0000000011₂ (3₁₀)
1111101011₂ (1003₁₀)

그러나

1111101001₂ (1001₁₀)
0000000011₂ (3₁₀)
1111101010₂ (1002₁₀)

사용 하지 않는 이유 가 있습니다 1000 * 1000 * 1000.

16 비트 int에서는 1000 * 1000오버플로가 발생합니다. 따라서 사용 1000 * 1000 * 1000하면 휴대 성이 줄어 듭니다.

32 비트 int를 사용하면 다음과 같은 오버플로가 발생합니다.

long long Duration = 1000 * 1000 * 1000 * 1000;  // overflow
long long Duration = 1000000000000;  // no overflow, hard to read

리드 값은 가독성, 이식성 정확성을 위해 대상 유형과 일치하도록 제안하십시오 .

double Duration = 1000.0 * 1000 * 1000;
long long Duration = 1000LL * 1000 * 1000 * 1000;

또한 e정확히로 표현할 수있는 값에 대한 간단한 표기법을 사용할 수도 있습니다 double. 물론 이것은 double정수 값을 정확하게 나타낼 수 있는지 여부를 알 수 있습니다-1e9보다 큰 값의 우려 사항. ( DBL_EPSILON참조 DBL_DIG).

long Duration = 1000000000;
// vs.
long Duration = 1e9;

가독성을 위해.

0 ( 1 000 000 000또는 1,000,000,000) 사이에 쉼표와 공백을 넣으면 구문 오류가 발생 1000000000하며 코드에 있으면 정확히 몇 개의 0이 있는지 알기가 어렵습니다.

1000*1000*1000우리 눈이 청크를 더 쉽게 처리 할 수 ​​있기 때문에 10 ^ 9임을 알 수 있습니다. 또한 컴파일러는 런타임 비용을 constant로 대체하기 때문에 런타임 비용이 없습니다 1000000000.


가독성을 위해. 비교 _를 위해 Java는 가독성을 높이기 위해 숫자를 지원합니다 ( Derek Foster의 제안 : Project Coin / JSR 334에 대한 이진 리터럴에 대한 응답 으로 Stephen Colebourne이 처음 제안했습니다 ). 하나는 1_000_000_000여기에 것이다.

가장 오래된 지원부터 최신까지 대략 연대순으로 :

It's a relatively new feature for languages to realize they ought to support (and then there's Perl). As in chux@'s excellent answer, 1000*1000... is a partial solution but opens the programmer up to bugs from overflowing the multiplication even if the final result is a large type.


Might be simpler to read and get some associations with the 1,000,000,000 form.

From technical aspect I guess there is no difference between the direct number or multiplication. The compiler will generate it as constant billion number anyway.

If you speak about objective-c, then 1000^3 won't work because there is no such syntax for pow (it is xor). Instead, pow() function can be used. But in that case, it will not be optimal, it will be a runtime function call not a compiler generated constant.


To illustrate the reasons consider the following test program:

$ cat comma-expr.c && gcc -o comma-expr comma-expr.c && ./comma-expr
#include <stdio.h>

#define BILLION1 (1,000,000,000)
#define BILLION2 (1000^3)

int main()
{
        printf("%d, %d\n", BILLION1, BILLION2);
}
0, 1003
$

Another way to achieve a similar effect in C for decimal numbers is to use literal floating point notation -- so long as a double can represent the number you want without any loss of precision.

IEEE 754 64-bit double can represent any non-negative integer <= 2^53 without problem. Typically, long double (80 or 128 bits) can go even further than that. The conversions will be done at compile time, so there is no runtime overhead and you will likely get warnings if there is an unexpected loss of precision and you have a good compiler.

long lots_of_secs = 1e9;

참고URL : https://stackoverflow.com/questions/40633059/why-write-1-000-000-000-as-100010001000-in-c

반응형