468,257 Members | 1,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

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 17911
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Griff | last post: by
8 posts views Thread by Tom | last post: by
8 posts views Thread by Madhusudan Singh | last post: by
2 posts views Thread by Alexander | last post: by
11 posts views Thread by Ben Blank | last post: by
12 posts views Thread by Chadwick Boggs | last post: by
13 posts views Thread by yb | last post: by
16 posts views Thread by luca bertini | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.