LinuxQuestions.org
Visit Jeremy's Blog.
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 10-07-2004, 01:05 PM   #1
browneyes
LQ Newbie
 
Registered: Sep 2004
Distribution: Suse 9.2
Posts: 21

Rep: Reputation: 15
Assembly, expressions..which order?


What is the order of precedence in assembly?

for like this expression:

i = a + 4 + b * 7 / c * 3


I tried to do it in C and I got answer of 24, when a=8, b=10, c=15.

But I keep getting 26 in assembly, I must have made a mistake in order.
 
Old 10-07-2004, 01:19 PM   #2
m00t00
Member
 
Registered: Sep 2004
Distribution: Slackware 10, Gentoo
Posts: 292

Rep: Reputation: 30
that aint assembly, unless you're talking about HLA...

/me despises HLA...
 
Old 10-07-2004, 01:35 PM   #3
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
the 'mistake' has beeen made by C.

Perhaps you have declared a, b and c as integer

and so the b*7/c is an integer division which has 4 as answer (instead of 4.6666666...)

You must declare all your variables float or double
and write the equation like that:

a + 4. + b * 7. / c * 3. = 26.0

Last edited by fiomba; 10-07-2004 at 01:36 PM.
 
Old 10-07-2004, 02:18 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Quote:
Originally posted by fiomba
the 'mistake' has beeen made by C.

Perhaps you have declared a, b and c as integer

and so the b*7/c is an integer division which has 4 as answer (instead of 4.6666666...)

You must declare all your variables float or double
and write the equation like that:

a + 4. + b * 7. / c * 3. = 26.0
Actually, with the way that promotion in C works, you can still get the correct answer by only changing the 7 into 7. No need to change them all:
Code:
itsme@itsme:~/C$ cat stuff.c
#include <stdio.h>

int main(void)
{
  int a = 8, b = 10, c = 15;

  printf("%f\n", a + 4 + b * 7. / c * 3);
  return 0;
}
itsme@itsme:~/C$ ./stuff
26.000000
 
  


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
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM
C: array expressions? centr0 Programming 5 07-06-2003 04:13 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

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

All times are GMT -5. The time now is 04:40 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