147

In code created by Apple, there is this line:

CMTimeMakeWithSeconds( newDurationSeconds, 1000*1000*1000 )

Is there any reason to express 1,000,000,000 as 1000*1000*1000?

Why not 1000^3 for that matter?

26
  • 47
    Clarity and readability of the code. You can't put , or ' as separators in C, so the next best thing is computing the value from a multiplication. ^ is an unrelated operator in C - exclusive OR. Commented Nov 16, 2016 at 13:21
  • 55
    it especially used for durations like : 2*60*60 .. its easy to notice that is for 2hours..
    – Idali
    Commented Nov 16, 2016 at 14:41
  • 53
    For one I suspect 1,000,000,000 is not valid syntax
    – paparazzo
    Commented Nov 16, 2016 at 15:18
  • 15
    By the way, modern programming languages have alternative way to write the same, e.g. in Swift 1_000_000_000. However, with time constants this is more difficult. It's more readable to write 30 * 60 (30 minutes in seconds) than to write 1800. There are actually languages that will allow you to write units, e.g. meters, allowing you to protect yourself against bad assignments.
    – Sulthan
    Commented Nov 16, 2016 at 16:05
  • 30
    ^ is an XOR, not an exponent or power operator.
    – 3Dave
    Commented Nov 16, 2016 at 18:35

8 Answers 8

203

One reason to declare constants in a multiplicative way is to improve readability, while the run-time performance is not affected. Also, to indicate that the writer was thinking in a multiplicative manner about the number.

Consider this:

double memoryBytes = 1024 * 1024 * 1024;

It's clearly better than:

double memoryBytes = 1073741824;

as the latter doesn't look, at first glance, the third power of 1024.

As Amin Negm-Awad mentioned, the ^ operator is the binary XOR. Many languages lack the built-in, compile-time exponentiation operator, hence the multiplication.

10
  • 14
    And in languages that do have an exponentiation operator, it isn't necessarily '^'. In Fortran, for instance, it's '**'.
    – jamesqf
    Commented Nov 16, 2016 at 17:28
  • 4
    You should also include a link pointing to the important caveat, given in answer below, from @chux : stackoverflow.com/a/40637622/1841533 (especially as the OP tagged "c", which is very susceptible to this 'righ-hand side operation seem to have all terms limited to a smaller type, and therefore the multiplication may overflow' problem). securecoding.cert.org/confluence/display/c/… may help avoiding those in the general case? Commented Nov 16, 2016 at 18:13
  • 4
    Also we should note that the computation is done at compile time. The C Standard requires the implementation to be able to compute constant expressions at compile-time for various features of the language and we can safely assume it is true when a constant expression is used like in this example.
    – ouah
    Commented Nov 16, 2016 at 18:32
  • 13
    Storing amount of memory as a double? That seems like a potential source of error.
    – JAB
    Commented Nov 16, 2016 at 19:25
  • 8
    @supercat I'm aware of that, but by using double you could have a case where, say, you want a portion of the memory range, you divide by x to get the size of the subrange... and suddenly you have a fractional byte, which may require additional logic to compensate for.
    – JAB
    Commented Nov 17, 2016 at 0:00
78

There are reasons not to use 1000 * 1000 * 1000.

With 16-bit int, 1000 * 1000 overflows. So using 1000 * 1000 * 1000 reduces portability.

With 32-bit int, the following first line of code overflows.

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

Suggest that the lead value matches the type of the destination for readability, portability and correctness.

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

Also code could simple use e notation for values that are exactly representable as a double. Of course this leads to knowing if double can exactly represent the whole number value - something of concern with values greater than 1e9. (See DBL_EPSILON and DBL_DIG).

long Duration = 1000000000;
// vs.
long Duration = 1e9;
6
  • 5
    Very important remark ! securecoding.cert.org/confluence/display/c/… may help in a lot of cases? Commented Nov 16, 2016 at 18:18
  • 2
    A double can exactly represent all integers up to 2^53 ≈ 9e15. Commented Nov 17, 2016 at 9:56
  • 5
    @EdgarBonet True that binary64 can represent whole number up to about 9e15. But C does not specify double use of binary64, even though it is very commonly used. Per the C spec, values up to 1e9 or so are exactly representable. It depends if you want to code to spec or rely on common practice. Commented Nov 17, 2016 at 15:14
  • 8
    @Patrick Both 1000 and 1000000000000 are integer constants. Each independently with have the type selected from int, long or long long. The compiler uses the first type of those 3 in which the integer constant fits. 1000 * 1000 * 1000 * 1000 is done with int math as each 1000 in an int. The product overflows with 32-bit int. 1000000000000 is certainly representable as a long long (or possibly narrower) - no overflow. The type of the target long long Duration does not affect this "right side of the =" detemrination. Commented Nov 17, 2016 at 18:25
  • 4
    Placing the wider type first in the multiplication is important. With 16-bit int, long x = 1000 * 1000 * 1000L; would overflow, while long x = 1000L * 1000 * 1000; would not. Commented Jul 24, 2017 at 15:03
74

Why not 1000^3?

The result of 1000^3 is 1003. ^ is the bit-XOR operator.

Even it does not deal with the Q itself, I add a clarification. x^y does not always evaluate to x+y as it does in the questioner's example. You have to xor every bit. In the case of the example:

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

But

1111101001₂ (1001₁₀)
0000000011₂ (3₁₀)
1111101010₂ (1002₁₀)
11
  • 3
    sir, i'm not clear how 1003^3 is 1003. Google and Mac Calculator shows 1000^3 = 1,000,000,000. can you explain?
    – MD.
    Commented Nov 16, 2016 at 13:23
  • 61
    The ^ operator means XOR in C/C++/Objective-C etc. In calculators it usually means x-to-the-y power. Commented Nov 16, 2016 at 13:25
  • 5
    Bah, the bits of 1000 and 3 do not overlap. This looks so wrong. Commented Nov 16, 2016 at 16:40
  • 22
    The bits do overlap. But not the 1's. :-] Commented Nov 16, 2016 at 16:45
  • 6
    @Yakk: indeed, it looks so wrong ! ... I hope many people won't think that "A^B" always gives A+B (but I fear some might...) Commented Nov 16, 2016 at 18:10
52

For readability.

Placing commas and spaces between the zeros (1 000 000 000 or 1,000,000,000) would produce a syntax error, and having 1000000000 in the code makes it hard to see exactly how many zeros are there.

1000*1000*1000 makes it apparent that it's 10^9, because our eyes can process the chunks more easily. Also, there's no runtime cost, because the compiler will replace it with the constant 1000000000.

5
  • 5
    FYI, there is a concept of digit separators that I learned about recently. Java has had it for a while now, and C# 7.0 may get it. I wish all languages would have this eye-candy feature. :) Commented Nov 16, 2016 at 15:40
  • 1
    Depending on the context using 1,000,000,000 wouldn't produce a syntax error, it would just mean something else. For example CMTimeMakeWithSeconds( newDurationSeconds, 1,000,000,000 )
    – Ross Ridge
    Commented Nov 16, 2016 at 18:15
  • 2
    @JMS10 C# already has it if you install VS15 preview version, can be written as 1_000_000_000 Commented Nov 16, 2016 at 19:08
  • 2
    Python is getting _ as the separator, too :) Commented Nov 16, 2016 at 21:31
  • 2
    And C++ recently got the ' separator, in C++14, so you can use 1'000'000'000. (It was chosen because 1,000,000,000 could be misinterpreted as the comma operator or 4 distinct parameters, and _1_000_000_000 is a valid (but probably bad) variable name.) Commented Nov 17, 2016 at 3:29
27

For readability. For comparison, Java supports _ in numbers to improve readability (first proposed by Stephen Colebourne as a reply to Derek Foster's PROPOSAL: Binary Literals for Project Coin/JSR 334) . One would write 1_000_000_000 here.

In roughly chronological order, from oldest support to newest:

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.

8
  • Many modern programming languages have the same, e.g. Swift. Nothing new.
    – Sulthan
    Commented Nov 16, 2016 at 16:06
  • AFAIK, this comes from Perl. PL/M used $ for the same purpose, e.g: 0100$0010B
    – ninjalj
    Commented Nov 17, 2016 at 0:52
  • 1
    It is fairly new, though. The Java feature is perhaps 5 years old. Most other languages supporting this syntax are pretty new -- Swift itself is only a few years old. Python adds support in 3.6, which hasn't been released yet.
    – johncip
    Commented Nov 17, 2016 at 3:02
  • 2
    Ada has supported underlines in integer literals for 33 years now. Commented Nov 17, 2016 at 10:02
  • 1
    @djechlin: I've taken the liberty of adding more information, roughly in chronological order. I was mistaken before, judging by the Project Coin thread, Stephen Colebourne probably took the idea of underscore in integer literals from Fandom and/or Ruby. Ruby probably took the idea from Perl, and Perl from Ada.
    – ninjalj
    Commented Nov 17, 2016 at 19:27
7

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.

4

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
$
1
  • 1
    @pjvandehaar I would not recommend learning a language by reading Wikipedia articles. Commented Dec 3, 2016 at 9:04
0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.