473,394 Members | 1,658 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.

Really Simple Math

I've declared o to be a double and c an int. No matter what the value of c,
o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?
Nov 17 '05 #1
9 1101
"Christopher Weaver" <we*****@verizon.net> wrote in
news:uT*************@TK2MSFTNGP15.phx.gbl:
I've declared o to be a double and c an int. No matter what the value
of c, o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?

c/100 performs integer division. If c is less than 100 then c/100 will
be a 0. If you want the value you expect, use

o = c / 100.0 now you do floating point division.
Any introductory programming book will go over this.

(BTW, o is a poor name of a variable of 1 character length.)
--
Dennis Roark

de***@sio.NOSPAMmidco.net
Starting Points:
http://sio.midco.net/denro/www
Nov 17 '05 #2
Thanks. I knew it was basic. In fact, as I read your reply I recalled the
point being made in my first semester c++ class. Years of Pascal has erased
much of that.

BTW, the 'o' was included for debugging purposes only.
"Dennis Roark" <de***@NOSPsio.midco.net> wrote in message
news:Xn***************************@216.196.97.142. ..
"Christopher Weaver" <we*****@verizon.net> wrote in
news:uT*************@TK2MSFTNGP15.phx.gbl:
I've declared o to be a double and c an int. No matter what the value
of c, o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?

c/100 performs integer division. If c is less than 100 then c/100 will
be a 0. If you want the value you expect, use

o = c / 100.0 now you do floating point division.
Any introductory programming book will go over this.

(BTW, o is a poor name of a variable of 1 character length.)
--
Dennis Roark

de***@sio.NOSPAMmidco.net
Starting Points:
http://sio.midco.net/denro/www

Nov 17 '05 #3
Christopher Weaver wrote:
I've declared o to be a double and c an int. No matter what the value of c,
o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?

No idea IF your claim is correct. However, I think what you really mean is "No
matter what the value of c (as long as it's less than 100), o is never greater
than 0."

The expression (c / 100) is apparently calculated using integer arithmetic,
which is perfectly reasonable since c and 100 are both integer expressions. The
integer result of this division will be zero when c is less than 100. I suggest
you test your "no matter what" claim with some larger values of c (e.g., 100,
101, 199, 200, 201, 299, etc.) and watch what happens.

-rick-
Nov 17 '05 #4
Personally I prefer to use:

o = c * 0.01;

Is it faster?
Perharps negligible.
"Christopher Weaver" <we*****@verizon.net> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Thanks. I knew it was basic. In fact, as I read your reply I recalled
the point being made in my first semester c++ class. Years of Pascal has
erased much of that.

BTW, the 'o' was included for debugging purposes only.
"Dennis Roark" <de***@NOSPsio.midco.net> wrote in message
news:Xn***************************@216.196.97.142. ..
"Christopher Weaver" <we*****@verizon.net> wrote in
news:uT*************@TK2MSFTNGP15.phx.gbl:
I've declared o to be a double and c an int. No matter what the value
of c, o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?

c/100 performs integer division. If c is less than 100 then c/100 will
be a 0. If you want the value you expect, use

o = c / 100.0 now you do floating point division.
Any introductory programming book will go over this.

(BTW, o is a poor name of a variable of 1 character length.)
--
Dennis Roark

de***@sio.NOSPAMmidco.net
Starting Points:
http://sio.midco.net/denro/www


Nov 17 '05 #5
Good point. It's so nice to have a group that pays attention and cares
enough to write.
"Rick Lones" <Wr******@YcharterZ.net> wrote in message
news:TO*****************@fe05.lga...
Christopher Weaver wrote:
I've declared o to be a double and c an int. No matter what the value of
c, o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?

No idea IF your claim is correct. However, I think what you really mean
is "No matter what the value of c (as long as it's less than 100), o is
never greater than 0."

The expression (c / 100) is apparently calculated using integer
arithmetic, which is perfectly reasonable since c and 100 are both integer
expressions. The integer result of this division will be zero when c is
less than 100. I suggest you test your "no matter what" claim with some
larger values of c (e.g., 100, 101, 199, 200, 201, 299, etc.) and watch
what happens.

-rick-

Nov 17 '05 #6
I think I like it better too.
"Altramagnus" <al*********@hotmail.com> wrote in message
news:42********@news.starhub.net.sg...
Personally I prefer to use:

o = c * 0.01;

Is it faster?
Perharps negligible.
"Christopher Weaver" <we*****@verizon.net> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
Thanks. I knew it was basic. In fact, as I read your reply I recalled
the point being made in my first semester c++ class. Years of Pascal has
erased much of that.

BTW, the 'o' was included for debugging purposes only.
"Dennis Roark" <de***@NOSPsio.midco.net> wrote in message
news:Xn***************************@216.196.97.142. ..
"Christopher Weaver" <we*****@verizon.net> wrote in
news:uT*************@TK2MSFTNGP15.phx.gbl:

I've declared o to be a double and c an int. No matter what the value
of c, o is never greater than 0 after the following line of code:

o = (c / 100);
Any ideas why?

c/100 performs integer division. If c is less than 100 then c/100 will
be a 0. If you want the value you expect, use

o = c / 100.0 now you do floating point division.
Any introductory programming book will go over this.

(BTW, o is a poor name of a variable of 1 character length.)
--
Dennis Roark

de***@sio.NOSPAMmidco.net
Starting Points:
http://sio.midco.net/denro/www



Nov 17 '05 #7
Altramagnus <al*********@hotmail.com> wrote:
Personally I prefer to use:

o = c * 0.01;

Is it faster?
Perharps negligible.


I don't think it's nearly as clear at a glance that that's dividing by
a hundred though. Just MHO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
i agree, but you can put a comment or something.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Altramagnus <al*********@hotmail.com> wrote:
Personally I prefer to use:

o = c * 0.01;

Is it faster?
Perharps negligible.


I don't think it's nearly as clear at a glance that that's dividing by
a hundred though. Just MHO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9
Altramagnus <al*********@hotmail.com> wrote:
i agree, but you can put a comment or something.


If you have to put a comment on one version to make it clearer, and you
haven't proved it to be faster, why do you prefer it? I'd take
readability over unproven speed any day.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

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

Similar topics

1
by: barnesc | last post by:
Hi! Here's a simple hashcash implementation in Python. 28 lines of actual code. Can be reduced to 17 lines for instructional purposes, if you don't want clustering, and use xrange() instead...
12
by: Alex Pierson | last post by:
Is there no easier way to convert a string into the variable that the string represents?
2
by: Webdiyer | last post by:
Hi, We all know that the return value of Math.Log(8,2) is 3,but how about (int)Math.Log(8,2)? On my machine,the return value of (int)Math.Log(8,2) is strange enough! it's not 3 but 2 ! I've...
1
by: macklin01 | last post by:
Hi, everybody. I'm trying to do some last cleaning up on the following php page I wrote: http://www.math.uci.edu/~pmacklin/Publications.php This URL parses an XML file of publications: ...
22
by: giordan | last post by:
Hi all! I've wrote this code: <script type="text/javascript"> var largImg; var altImg; var txtTop = '<b>Ottima scelta!</b> Ora compila il form e premi "Ricevi banner". Il...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.