473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

counting fractional part of a real number

1
Any who can help!!
let i have a number 16.0230045
i want to count the fractional part of this number means i want to get the number of digits of the fractional part of this number and store each digit in an array as well.
i.e i have a[size];//where size is the number of digits of the fractional parts of my number how can i do that using C++.
finally i should have my array to hold all the digits.
Remember my number can be type float ,or double.
Dec 16 '06 #1
2 3995
Manjiri
40
Any who can help!!
let i have a number 16.0230045
i want to count the fractional part of this number means i want to get the number of digits of the fractional part of this number and store each digit in an array as well.
i.e i have a[size];//where size is the number of digits of the fractional parts of my number how can i do that using C++.
finally i should have my array to hold all the digits.
Remember my number can be type float ,or double.


Hello here i have the solution... Plz see whether this is required by you....

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2.  
  3. int main()
  4. {
  5. int i=0,j=0,cnt=0;
  6. char ch[20],ch1[20];
  7. cout<<"Enter the floating point value..."<<endl;
  8. cin>>ch;
  9. while(ch[i]!='.')
  10. {
  11. i++;
  12. }
  13. i++;
  14. while(ch[i]!='\0')
  15. {
  16. ch1[j]=ch[i];
  17. cnt++;
  18. i++;
  19. j++;
  20. }
  21. cout<<"The fractional part is"<<endl;
  22. cout<<ch1<<endl;
  23. cout<<"Number of digits present are..."<<endl;
  24. cout<<cnt<<endl;
  25. return 0;
  26. }
Dec 19 '06 #2
macklin01
145 100+
Any who can help!!
let i have a number 16.0230045
i want to count the fractional part of this number means i want to get the number of digits of the fractional part of this number and store each digit in an array as well.
i.e i have a[size];//where size is the number of digits of the fractional parts of my number how can i do that using C++.
finally i should have my array to hold all the digits.
Remember my number can be type float ,or double.
Careful. Remember that there's floating point error, and you most likely do not have precisely the floating point number 16.0230045. More likely, you have something like

16.02300450000018945
16.023004500328585832
16.02300450039596595

etc. You'll need to choose a cutoff precision for this problem.

Once you decide upon that, I recommend checking out the modf function, which separates a double into its integer and fractional parts. After that, you could use the sprintf() function to put it into a string array.

Alternatively, you could use sprintf() to put the entire real/float/double into a character string, start looking for the decimal point in a way similar to that posted above. Note that regardless, you'll need to choose a precision. e.g.,
Expand|Select|Wrap|Line Numbers
  1. double some_number = 16.0230045;
  2. char str [1024];
  3. sprintf( str, "%3.8f", some_number );
  4. // count digits preceding and following the decimal point. 
  5. // remove the trailing zeros, if decided. 
  6. // etc.
  7.  
I hope that's helpful. Floating point error can be tricky, but you'll get the hang of it very, very quickly. -- Paul
Dec 19 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
5
by: Michael Søndergaard | last post by:
I Pascal there are a function for retriving the fractional part of a real number (Frac), I can't find any in the dotnet framework. The function needs to remove the integer part and keep only the...
3
by: Dragon | last post by:
Helo, There're so many methods for rounding in .NET, but I can't find any that will just drop fractional part of a number, e.g: -3.9 => -3. I tried: CInt(-3.9) Int(-3.9) Math.Round(-3.9)
2
by: Bob | last post by:
Greetings All, Is there a simple way of suppressing the fractional part of a timespan At the moment I use one time span to populate another minus the fractional part .. this is a bit clunky. ...
10
by: schaefer.mp | last post by:
Does anyone know of an approximation to raising a negative base to a fractional exponent? For example, (-3)^-4.11111 since this cannot be computed without using imaginary numbers. Any help is...
12
by: bsabiston | last post by:
Hi, I'm trying to get the fractional part of a floating point number. I've tried fmod() and modf(), and while both do work, they also occasionally return 1.0 for the fractional part of the number...
4
prn
by: prn | last post by:
Hi folks, I've got another (inherited) puzzle that I don't understand. A report that I need to modify contains a subreport that lists a variable number of items in its detail section and then...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.