Re: getting avg of long and std::vector::size_type

eed132_at_psu.edu
Date: 03/08/05


Date: 7 Mar 2005 22:42:02 -0800

Rolf Magnus already told you what you need to do, I just thought I'd
add a clarification point.

C and C++ just look at and evaluate one part of an expression at a
time. It evaluates that, then moves to the next operation. In the code:
  int a, b;
  float c = a/b;
the compiler starts by evaluating a/b. The only information it pays
attention to at this stage is what is in 'a/b'. It sees that a is an
int and b is an int, so it performs integer division on a and b. (This
gives 0 is a < b since it truncates -- rounds down -- any decimal
portion.) Only after getting this (integer) result does it convert it
to a floating point number and store it in c.

Rolf's fix, 'float c = static_cast<float>(a) / b', changes the
evaluation sequence. Now the compiler starts with the cast. So the
first step is to convert a to a float. Then it looks at the division.
Now the division is a float divided by an integer. In this case, the
C/C++ rules tell it to promote the integer to a float and do floating
point division, which is then stored in c. This gives the right answer.
(But again, I would reccomend taking Old Wolf's advice and using
double.)

One more, this time more complicated example:
  int a, b, c;
  int d = (a / b) / (static_cast<float>(c));
The sequence of evaulation is:
1. The compiler looks at 'a/b' and does integer division since both
operands are ints (call this intermediate result r1)
2. The compiler looks at 'static_cast<float>(c)' and performs the cast
(call this converted value r2)
3. The compiler looks at r1/r2. r1 is an int and r2 is a float, so it
converts r1 to a float and does floating point division. (call the
quotient r3)
4. Finally, the compiler looks at the assignment d = r3. d is an int,
so it demotes r3 to an integer and stores the value.

(Actually, as a technical note, I think the standard leaves the order
of steps 1 and 3 undefined, so the compiler could do it either way. I'm
not positive about that, but I think that's the way it is.)



Relevant Pages

  • Re: cpu type idea
    ... compiler related recently. ... int main ... float a; ... just to parallelize the vectror and tensor operations. ...
    (alt.lang.asm)
  • Re: matrix stuff (solving b = A*x) --> using numerical recipes
    ... Numerical recipes (can be seen on ... Ok, I added it now (actually I also had it at some earlier moment, but then I removed it since I couldn't see any compiler warnings/errors without stdio.h). ... void banmul(float **a, unsigned long n, int left, int right, float x, float b); ...
    (comp.lang.c)
  • Re: problem with cast and unions
    ... Think of it as a compiler for Small programs. ... to a Small "cell" (actually an int). ... can anyone please tell me how is the structure assignment implemented ...
    (comp.lang.c)
  • Re: OT: C++ overloading operators
    ... float *a,*b; ... void bar1(float * restrict a, const float * restrict b, float c, int N) ... If you're using the intel compiler, ...
    (comp.dsp)
  • Re: Simple output problem
    ... >> int main ... > a/b?", returning either a zero or one for true or false, but doesn't ... > value of a/b and place it into the variable c. ... you might get a compiler warning depending on the compiler. ...
    (comp.lang.c)