Floating point numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Floating point numbers

 
0
  #1
Dec 29th, 2004
i got my program to do stuff, but i wondered if i can take it to the next level...

when a user enters a number of 1458.125478 i got it to do abs.... google is soo good!! but now im wondering if you could display just + or - depending on what is entered. I tried creating an int for the sign to go into but it just doesnt want to know!

then i thought about a string so i went down that route not knowing that you cant combine a string and a float together!

so i had another idea

[php]

int main(){
float n; //n is the number entered

cout << " please input your number with the sign";
cin >> n; // prints everything

cout << n<<endl;

return 0;

}

[/php]

is it possible to put the float which is entered into an array? that way i can chose what is printed out on the screen?

... unless anyone else has other ideas which i welcome
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Floating point numbers

 
0
  #2
Dec 29th, 2004
Originally Posted by Acidburn
but now im wondering if you could display just + or - depending on what is entered.
If it's less than zero, print '-'; otherwise, print '+'.

Originally Posted by Acidburn
is it possible to put the float which is entered into an array?
Of course.
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. float value[3];
  6. int i;
  7. for ( i = 0; i < 3; ++i )
  8. {
  9. std::cout << "Please input your number with the sign: ";
  10. std::cin >> value[i]; // put user-entered value into an array element
  11. if ( value[i] < 0 )
  12. {
  13. std::cout << "-" << '\n';
  14. }
  15. else
  16. {
  17. std::cout << "+" << '\n';
  18. }
  19. }
  20. return 0;
  21. }
  22.  
  23. /* my input/output
  24. Please input your number with the sign: 1458.125478
  25. +
  26. Please input your number with the sign: -2.3
  27. -
  28. Please input your number with the sign: 0
  29. +
  30. */
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Floating point numbers

 
0
  #3
Dec 29th, 2004
float value[3];
int i;
for ( i = 0; i < 3; ++i )
{
cout << "Please input your number with the sign: ";
cin >> value[i]; // put user-entered value into an array element
if ( value[i] < 0 )

could you explain that bit a little more so i can try and follow whats going on. From my interpration

you got a float thats an array - true?
you print out the array using the loop i

but one thing i dont get is how do you stop it printing the rest out ? just the sign
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Floating point numbers

 
0
  #4
Dec 29th, 2004
>could you explain that bit a little more so i can try and follow whats going on.
  1. float value[3]; // an array of 3 floats
  2. int i; // a loop counter
  3. for ( i = 0; i < 3; ++i ) // loop 3 times
I can't imagine you're having trouble with that.
  1. cin >> value[i]; // put user-entered value into an array element
Is this comment confusing?

>you print out the array using the loop i

No. I print a plus or minus. I do so depending on whether the value entered is less than zero.
  1. if ( value[i] < 0 )
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Floating point numbers

 
0
  #5
Dec 29th, 2004
ok im going back to my book! I should of known how to do that and i cant belive it was that easy...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #6
Dec 30th, 2004
Intresting topic we have here, I'm wondering if theres a away to just print out the values after the decimal point.

This is what I thought -

If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Floating point numbers

 
0
  #7
Dec 30th, 2004
Originally Posted by happyHour
Intresting topic we have here, I'm wondering if theres a away to just print out the values after the decimal point.

This is what I thought -

If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter
crude but effective idea would be to change the string to an int. Why well with my little depth of c++ itsall i can think of ... But think about it for a moment you convert it to an int ... What can an int hold?? 4 bytes of real numbers... Whole real numbers! so in feect you'd loose your decimal point. All you have to do then is to take away the number left in the string with the one you created - Hence leaving you with the decimal parts.. .Note that you won't get the 0. xxxxxx Not sure how that works ...

Nor could i code this without research , have a go if your still struggling the forum is always here
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Floating point numbers

 
0
  #8
Dec 30th, 2004
>I'm wondering if theres a away to just print out the values after the decimal point.
There's a library function in <cmath> called modf that breaks up a floating-point value into its constituent pieces. Alternatively, you could copy the value to an int (which truncates the fractional part) and subtract that from the original floating-point value. Either way, the result is the fractional part of the floating-point value, and you still have access to the whole part.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Floating point numbers

 
0
  #9
Dec 31st, 2004
I have a feeling that in the iostream classes there is a formatting option to set the precision and decimal places. I have never used them myself so i couldnt tell you what they were but a google search should reveal it.

I think it was something along the lines of

  1. cout.flags( options ... )
  2. or
  3. cout << options... << text

If you cant find it try searching for a hexadecimal number tutorial as hex is a formatting flag which tells cout to display numbers in hexadecimal.... as far as i remember...
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #10
Jan 1st, 2005
Thanks for the replies, just wondering if you can split up a string of numbers? ... say all the numbers before the decimal point...into string1
and all the others after the decimal point into string 2?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC