Very confused,,,, divide in C

V

VijaKhara

Hi all

In my codes, I have something a=b+(1/3); (a and b are two double
variables)

The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.

How to over come this problem?

Thanks
 
J

Jens Thoms Toerring

VijaKhara said:
In my codes, I have something a=b+(1/3); (a and b are two double
variables)
The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.

No, you have to replace 1/3 by 1.0/3 or 1/3.0 or 1.0/3.0.
If both operands are integers so-calles integer division
is used where the result is only the integer part of the
result of the division. But if one (or both) of the num-
bers is a floating point value "normal" division is used.

Regards, Jens
 
W

Walter Roberson

VijaKhara said:
In my codes, I have something a=b+(1/3); (a and b are two double
variables)
The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.
How to over come this problem?

a=b+(1./3)

or

a=b+(1/3.)

or

a=b+(1./3.)


The problem that you are encountering is that 1 and 3 are both
integers, so it is doing an integer division. C does -not- look
at the surrounding expressions in order to determine which result
type you "meant": it only looks at the types of the two operands
of the operator. (However, the two operands of an operator are not always
the most obvious two closest variables: you have to take into
account operator precedence. For example, in a=b*c/d the
operands of the division are not c and d: they are (b*c) and d
because the multiplicative operators associate left-to-right.)
 
D

Default User

VijaKhara said:
Hi all

In my codes, I have something a=b+(1/3); (a and b are two double
variables)

But 1 and 3 are not doubles, they are integers (ints to be specific).
So integer division is performed. Look up in your text what happens
with integer division.
How to over come this problem?


Figure out how to make that division floating point. Your text should
address that.




Brian
 
M

Martin Ambuhl

VijaKhara said:
Hi all

In my codes, I have something a=b+(1/3); (a and b are two double
variables)

The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.

How to over come this problem?

1 is an integer
3 is an integer
1/3 is done with integer arithmetic, and has the value 0

any of the following (and many others) would do floating point arithmetic:
1./3
1/3.
(double)1 / 3

All of the following (and others) are floating point, and all used with
an arithmetic operator lead to floating point computation
1., 1.0, (float) 1, (double) 1, (long double) 1,
3., 3.0, (float) 3, (double) 3, (long double) 3

It is a good idea to check the FAQ before posting questions. In fact,
it is expected that you would do so. You might want to look at, for
example,
Q 3.15 <http://c-faq.com/expr/truncation1.html>
"Why does the code

double degC, degF;
degC = 5 / 9 * (degF - 32);

keep giving me 0?"
 
K

Keith Thompson

Martin Ambuhl said:
1 is an integer
3 is an integer
1/3 is done with integer arithmetic, and has the value 0

any of the following (and many others) would do floating point arithmetic:
1./3
1/3.
(double)1 / 3

Personally, I'd prefer "1.0" and "3.0" rather than "1." and "3.".
It's true that C allows a trailing decimal point in a floating-point
constant; I just think the more verbose forms look better and are
visually less ambiguous. That's just my opinion, not a criticism of
Martin's code, which is perfectly valid.

Note that it may be tempting to write something like:

double a, b
a = b + (double)(1/3);

but that won't work. In almost all cases, the type of an expression
is determined by the expression itself, not by the context in which it
appears. In this case, (1/3) performs an integer division; converting
it to double *after* the division has been performed doesn't change
that. This rule can cause some confusion, as in this example, but
once you understand the rule, it makes expressions easier to
understand. (It also makes the compiler's job easier.)
 
M

maud_dib

Hi all

In my codes, I have something a=b+(1/3); (a and b are two double
variables)

The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.

How to over come this problem?

Thanks

I think it is because 1 and 3 are being treated like integers which
results in being truncated to 0. Try using 2 other double variables
instead, double c=1, double d=3... then c/d should give you what you
are looking for.
 
J

Jack Klein

I think it is because 1 and 3 are being treated like integers which

No, the integer constants '1' and '3' ARE of type int. They don't
have to be "treated" like ints, they are ints.
results in being truncated to 0. Try using 2 other double variables

No, nothing is being truncated. 0 is the absolute correct and 100%
accurate result of the integer division.
instead, double c=1, double d=3... then c/d should give you what you
are looking for.

There is no need to create and initialize objects. See most of the
other replies in this thread for details on how to use floating point
constants which by default have type double.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
P

Peter Nilsson

Jack Klein said:
maud_dib said:
[The sub-expression (1/3)] results in being truncated
to 0. ...

No, nothing is being truncated.

'This is often called "truncation toward zero"'

True, for C90, it needn't apply if either division operand
is negative, but it does apply if the operands are 1 and 3.
 
V

Vinoj

Hi all

In my codes, I have something a=b+(1/3); (a and b are two double
variables)

The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.

How to over come this problem?

Thanks

Since a=b+(1/3) you try this and this will work out if you want to be
with integer.

a=(3*b+1)/3, I think this is a simple maths solution rather than a
computing solution.

Thanks, Regards,
Vinoj
 
F

Flash Gordon

Vinoj wrote, On 16/05/07 06:29:
Since a=b+(1/3) you try this and this will work out if you want to be
with integer.

a=(3*b+1)/3, I think this is a simple maths solution rather than a
computing solution.

It is definitely a computing problem since although what you have
suggested is mathematically fine it is not fine as a computing solution.
It will suffer from overflow a lot earlier.
 
O

Old Wolf

[ re. (1/3) ]
results in being truncated to 0.

No, nothing is being truncated. 0 is the absolute correct and 100%
accurate result of the integer division.

C99 6.5.5
[#6] When integers are divided, the result of the
/ operator is the algebraic quotient with any
fractional part discarded. (88)

(88)This is often called "truncation toward zero".

Note that it talks about "fractional part" being discarded,
which is truncation in normal scientific parlance (and
also in the footnote!).

Mathematically, the ring of integers doesn't even
have division. There is the Euclidean algorithm,
but that gives -1/3 = -1 remainder 2, which is not
what the C standard mandates.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,041
Members
48,769
Latest member
Clifft

Latest Threads

Top