GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Feb-2004, 21:55
ewallo ewallo is offline
New Member
 
Join Date: Feb 2004
Posts: 4
ewallo is on a distinguished road

modulus arithmetic


I have a quick question about using modulus arithmetic in calculations involving float variables. What data type should I use? I am trying to perform the following calculation, but I am getting a illegal error. Any sugge
stions would be appreciated.

code:
CPP / C++ / C Code:
cout << " fifty:  " << changeDue / 50 % 50 << endl;
I am getting the following error:

CODE.cpp(42) : error C2296: '%' : illegal, left operand has type 'float'
Error executing cl.exe.

Thanks,

ewallo
Last edited by dsmith : 22-Feb-2004 at 21:58. Reason: Added C syntax highlighting.
  #2  
Old 22-Feb-2004, 22:05
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Ewallo. One quick thing, when you post c/c++ code if you start it with [c] and end it with [/c] it will highlight your code and make it much more readable

C/C++ makes some assumptions about your results unless you use typecasting, so when you perform:

CPP / C++ / C Code:
changeDue / 50

C defaults to give you a floating result. In order to perform integer division, you need to typecast your result as so:

CPP / C++ / C Code:
(int) (changeDue / 50)

Try something more like this

CPP / C++ / C Code:
cout << " fifty:  " <<  (int) (changeDue / 50) % 50 << endl;

Also, WaltP has justed posted an excellent thread here wherein he talks a bit about parenthesis. If you get a chance it helps for *human* readability.

Good luck
  #3  
Old 22-Feb-2004, 22:10
ewallo ewallo is offline
New Member
 
Join Date: Feb 2004
Posts: 4
ewallo is on a distinguished road
Quote:
Originally Posted by dsmith
Hi Ewallo. One quick thing, when you post c/c++ code if you start it with [c] and end it with [/c] it will highlight your code and make it much more readable

C/C++ makes some assumptions about your results unless you use typecasting, so when you perform:

CPP / C++ / C Code:
changeDue / 50

C defaults to give you a floating result. In order to perform integer division, you need to typecast your result as so:

CPP / C++ / C Code:
(int) (changeDue / 50)

Try something more like this

CPP / C++ / C Code:
cout << " fifty:  " <<  (int) (changeDue / 50) % 50 << endl;

Also, WaltP has justed posted an excellent thread here wherein he talks a bit about parenthesis. If you get a chance it helps for *human* readability.

Good luck

dsmith,

Thanks for the reply. I will give it a try. I will also make sure to mark my code correctly next time. Thanks again!!
  #4  
Old 23-Feb-2004, 00:35
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ewallo
What data type should I use? I am trying to perform the following calculation, but I am getting a illegal error. Any sugge
stions would be appreciated.
...
I am getting the following error:

CODE.cpp(42) : error C2296: '%' : illegal, left operand has type 'float'
Error executing cl.exe.
The error is because the % operator only works on integers. To deal with floating point values, use the fmod() function from math.h, which takes two doubles.

fmod(13.0, 2.25) is 1.75
fmod(13.0, 2.50) is 0.5
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.