LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2013, 08:51 PM   #1
ceyx
Member
 
Registered: May 2009
Location: Fort Langley BC
Distribution: Kubuntu,Free BSD,OSX,Windows
Posts: 342

Rep: Reputation: 59
C++: Why does "100 x 9/5" differ from "100 x (9/5)"?


I was experimenting with some arithmetic in Bloodshed Dev C++, and I found that:

9/5 = 1, and
(9/5) = 1

This makes sense to me because you are dividing two integer data-types, which will produce a truncated integer as opposed to the "correct" answer of 1.8. However, I became confused when I got the following outputs:

100 * 9/5 = 180, and
100 * (9/5) = 100.

The operation producing "180" was the answer I was looking for, but my code normally follows the latter's format. I'm wondering as to why the brackets make a difference here.
 
Old 05-20-2013, 09:00 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
100 * 9 / 5 is evaluated in sequence.
100 * 9 = 900 (stored in a temporary value)
900 / 5 = 180

The other one:
(9/5) = 1 (stored in a temporary integer value)
100 * 1 = 100

jlinkels
 
3 members found this post helpful.
Old 05-20-2013, 09:08 PM   #3
ceyx
Member
 
Registered: May 2009
Location: Fort Langley BC
Distribution: Kubuntu,Free BSD,OSX,Windows
Posts: 342

Original Poster
Rep: Reputation: 59
Thanks for the helpful reply!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Zenwalk Linux 7.2 aims for "100% Slackware compatibility" LXer Syndicated Linux News 0 10-15-2012 09:00 PM
LXer: Mysql Used By Majority Of "AlwaysOn 100" Innovators LXer Syndicated Linux News 0 07-28-2006 05:54 AM
LXer: $100 PC project will "invigorate Linux desktop push" LXer Syndicated Linux News 0 01-30-2006 10:46 PM
pam_succeed_if: requirement "uid < 100" not met by user Phonics3k Linux - General 3 12-17-2004 09:13 AM
how would I list a sequence of files as "seq.$F4.ext : 1-100" BrianK Programming 0 07-12-2004 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration