472,954 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

modulo for floats?

I'm writing a basic piece of software that will tell me how many shares
of stock I can buy if the shares cost X each and I have Y to spend.

I have it coded as:

int main()
{

float price, investment;

cout << "enter stock price:";
cin >> price;
cout << "enter total amount available to invest:";
cin >> investment;
cout << "you can buy " << investment%price << "shares.";

return 0;
}

but my compiler doesn't like the modulo with floats and wont compile.
Technically I could change them to ints, but stock prices aren't always
rounded to the nearest dollar.

Mar 4 '06 #1
4 18229
in*****@yahoo.com wrote:
I'm writing a basic piece of software that will tell me how many shares
of stock I can buy if the shares cost X each and I have Y to spend.

I have it coded as:

int main()
{

float price, investment;

cout << "enter stock price:";
cin >> price;
cout << "enter total amount available to invest:";
cin >> investment;
cout << "you can buy " << investment%price << "shares.";

return 0;
}

but my compiler doesn't like the modulo with floats and wont compile.
Technically I could change them to ints, but stock prices aren't always
rounded to the nearest dollar.

Look up `fmod()'.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 4 '06 #2
<in*****@yahoo.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
cout << "enter stock price:";
cin >> price;
cout << "enter total amount available to invest:";
cin >> investment;
cout << "you can buy " << investment%price << "shares."; but my compiler doesn't like the modulo with floats and wont compile.


Why do you think modulo is appropriate here?
Mar 4 '06 #3
in*****@yahoo.com wrote:
I'm writing a basic piece of software that will tell me how many
shares of stock I can buy if the shares cost X each and I have Y to
spend.

I have it coded as:

int main()
{

float price, investment;

cout << "enter stock price:";
cin >> price;
cout << "enter total amount available to invest:";
cin >> investment;
cout << "you can buy " << investment%price << "shares.";

return 0;
}

but my compiler doesn't like the modulo with floats and wont compile.
Technically I could change them to ints, but stock prices aren't
always rounded to the nearest dollar.


It doesn't sound to me like you want % (actually the remainder
operator). Do a division first, then use floor() to get the nearest
whole number of shares less than or equal to that value.

Brian

Mar 4 '06 #4
in*****@yahoo.com wrote:
I'm writing a basic piece of software that will tell me how many shares
of stock I can buy if the shares cost X each and I have Y to spend.

I have it coded as:

int main()
{

float price, investment;

cout << "enter stock price:";
cin >> price;
cout << "enter total amount available to invest:";
cin >> investment;
cout << "you can buy " << investment%price << "shares.";

return 0;
}

but my compiler doesn't like the modulo with floats and wont compile.
Technically I could change them to ints, but stock prices aren't always
rounded to the nearest dollar.


Prices should not be expressed in floating-point values
because of round-off errors. You'd be better off storing
the prices as integers. If they are not rounded to the
nearest dollar, why not store them as values "in cents"?

HTH,
- J.
Mar 4 '06 #5

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

Similar topics

5
by: Griff | last post by:
Test program: =============================================================== t = 5.8 interval = 2.0 while t < 6.1: print "%s mod %s = %s " % (t, interval, t % interval ) t += 0.1...
8
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
8
by: Madhusudan Singh | last post by:
Is it possible to convert a very long list of strings to a list of floats in a single statement ? I have tried float(x) and float(x) but neither work. I guess I would have to write a loop if...
2
by: Alexander | last post by:
I played a little bit with modulo cause I wanted to implement a small parser for functions as I stumbled over a calculation error. First I thought the reason might be a type conversion I did...
11
by: Ben Blank | last post by:
I have a loop which iterates over an array in a particular order: for (j = 0; j < 16; j++) T = ...; The loop proceeds normally for j = 0 through j = 4, but gives an IndexOutOfRangeException at...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
13
by: yb | last post by:
Hi, Is there a CSS method to clear a float such that it aligns with the left content edge. For example: X X X X X X X X
16
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
0
by: Matthieu Brucher | last post by:
2008/11/5 L V <somelauw@yahoo.com>: Hi, I don't think the Python developers list is th best list to post this kind of question. What version of Python did you use for this test? Matthieu
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.