473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Very confused,,,, divide in C

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

May 14 '07 #1
13 57687
VijaKhara <Vi*******@gmail.comwrote:
The result always is that (1/3)=0
Right. The integer result of 1/3 is 0. What you want is 1.0/3.0.
How to over come this problem?
Sounds like you might want to check the textbook again.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
May 14 '07 #2
VijaKhara <Vi*******@gmail.comwrote:
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
May 14 '07 #3
In article <11**********************@h2g2000hsg.googlegroups. com>,
VijaKhara <Vi*******@gmail.comwrote:
>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.)
--
Programming is what happens while you're busy making other plans.
May 14 '07 #4
VijaKhara wrote:
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
May 14 '07 #5
VijaKhara wrote:
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?"

May 14 '07 #6
Martin Ambuhl <ma*****@earthlink.netwrites:
VijaKhara wrote:
>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
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 14 '07 #7
On May 14, 11:35 am, VijaKhara <VijaKh...@gmail.comwrote:
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.

May 15 '07 #8
On 14 May 2007 17:00:03 -0700, maud_dib <cf*******@gmail.comwrote in
comp.lang.c:
On May 14, 11:35 am, VijaKhara <VijaKh...@gmail.comwrote:
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
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
May 15 '07 #9
Jack Klein <jackkl...@spamcop.netwrote:
maud_dib <cfatta...@gmail.comwrote in comp.lang.c:
[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.

--
Peter

May 15 '07 #10
On May 14, 11:35 pm, VijaKhara <VijaKh...@gmail.comwrote:
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

May 16 '07 #11
VijaKhara wrote:
>
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.0 / 3;
or
a = b + 1 / 3.0;
or
a = b + 1.0 / 3.0;

(1/3) does an integer divide because both operands are integers.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #12
Vinoj wrote, On 16/05/07 06:29:
On May 14, 11:35 pm, VijaKhara <VijaKh...@gmail.comwrote:
>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.
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.
--
Flash Gordon
May 16 '07 #13
On May 15, 2:24 pm, Jack Klein <jackkl...@spamcop.netwrote:
maud_dib <cfatta...@gmail.comwrote in comp.lang.c:
[ 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.

May 17 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: KTyson9426 | last post by:
I'm trying to create a table that has a column named "timestamp"... for some reason unknown to me... the statement barfs all over the "timestamp" section of it and gives me "ERROR: parse error...
6
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */...
3
by: AAA | last post by:
I want to start programming in MS Visual C++. I understand that .NET is required? Or do I have to get MSStudio to integrate my work into the NET frame work? And any idea when VS 2005 is going to...
5
by: Shapper | last post by:
Hello, I am working in a web site where all the code is placed in aspx.vb files. After a while I realized that many functions included in my aspx.vb files where common to all pages. I...
7
by: Lance | last post by:
hi all, in the immediate window ( typing: ? Val("&H" & "4124EC7DE6666666") and in calc, if i convert 4124EC7DE6666666 to decimal then i get 4.6941367371074376E+18 (or 4694136737107437158)....
2
by: jhcorey | last post by:
I have the code below that works fine. Note that CellSet does not have a constructor, so I don't have a default way of initializing it. If I try to do something else in the catch block, either...
3
by: Evan | last post by:
So I have two versions of a program (these are complete programs, not excerpts): Version 1: template <class T > void foo() { return bar( T() ); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.