473,434 Members | 1,791 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,434 software developers and data experts.

Help with fraction to decimal code in C++

Hello,

The following C++ code is what I am trying to use in a fraction class to convert a fraction to a decimal. the num and den are both ints. When I make a call to the function (f1.decimal_value()), the return value is 0.

Can any one help with this problem?




Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value()
  2.     {
  3.         double decimal_num=num;
  4.         double decimal_den=den;
  5.         double decimal=(decimal_num)/(decimal_den);
  6.         return decimal;
  7.     }
Apr 3 '07 #1
19 15262
sicarie
4,677 Expert Mod 4TB
Hello,

The following C++ code is what I am trying to use in a fraction class to convert a fraction to a decimal. the num and den are both ints. When I make a call to the function (f1.decimal_value()), the return value is 0.

Can any one help with this problem?




Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value()
  2.     {
  3.         double decimal_num=num;
  4.         double decimal_den=den;
  5.         double decimal=(decimal_num)/(decimal_den);
  6.         return decimal;
  7.     }
You don't pass num and den to the function, I'm guessing they get initialized to something weird - did you try printing them out to see what values they had in the function?

Another option is that the compiler might be trying to 'optimize' your code, seeing that num and den are ints, and converting decimal_num and decimal_den to ints without telling you - you could try (if the above doesn't work) something like:

double decimal_num = num*1.0; // makes sure a double is put in decimal_num
Apr 3 '07 #2
You don't pass num and den to the function, I'm guessing they get initialized to something weird - did you try printing them out to see what values they had in the function?

Another option is that the compiler might be trying to 'optimize' your code, seeing that num and den are ints, and converting decimal_num and decimal_den to ints without telling you - you could try (if the above doesn't work) something like:

double decimal_num = num*1.0; // makes sure a double is put in decimal_num
I think you were right. After printing num and den bo remain ints. I tried to use the *1.0 but no-joy. Is there another way to convert num and den to double or float just in that function?

Thanks in advance
Apr 3 '07 #3
sicarie
4,677 Expert Mod 4TB
And they're being passed/accessed correctly? (You didn't say anything, so I'm just checking.)
Apr 3 '07 #4
And they're being passed/accessed correctly? (You didn't say anything, so I'm just checking.)
Sorry, the function is being called correctly, but the num and den are remaining as ints. The answer that is given is 0. I am not sure how to check other then cout the decimal_num and decimal_den to the screen during the operation.

If I enter 2/3 the dispaly shows
2 // cout<decimal_num<< endl;
3 // cout <<decimal_den<<endl;
decimal vaule is 0.
Apr 3 '07 #5
sicarie
4,677 Expert Mod 4TB
I know I'm beating a dead horse here, but please just try this for me:

Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value()
  2. {
  3.     double decimal_num=num;
  4.     cout << num << '\t' decimal_num << endl;
  5.     double decimal_den=den;
  6.     cout << den << '\t' << decimal_num << endl;
  7.     double decimal=(decimal_num)/(decimal_den);
  8.     cout << decimal << endl;
  9.     return decimal;
  10. }
Apr 3 '07 #6
I know I'm beating a dead horse here, but please just try this for me:

Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value()
  2. {
  3.     double decimal_num=num;
  4.     cout << num << '\t' decimal_num << endl;
  5.     double decimal_den=den;
  6.     cout << den << '\t' << decimal_num << endl;
  7.     double decimal=(decimal_num)/(decimal_den);
  8.     cout << decimal << endl;
  9.     return decimal;
  10. }

No luck - I am getting an overloaded << error. But you are trying to display num and decimal_num after decimal_num= num?

I am knew to C++ code - not sure I understand.
Apr 3 '07 #7
sicarie
4,677 Expert Mod 4TB
Ok, that answered my question. Your problem lies here:

Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value()
  2.  
You are violating a rule of 'scope'. The problem is that in your main, you call decimal_value(), so memory for this function is being created. However, when num and den are created, they are given space in this new decimal_value() memory space - they are not the same num and den that are in your main(). To get them to be the same, you need to pass num and den to the function.

Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value(int num, int den)
  2.  
This gives num and den values, and from your main you call

Expand|Select|Wrap|Line Numbers
  1. double return_value;
  2. return_value = decimal_value(num, den);
  3.  
That should help clear it up.
Apr 3 '07 #8
I know I'm beating a dead horse here, but please just try this for me:

Expand|Select|Wrap|Line Numbers
  1. double fraction::decimal_value()
  2. {
  3.     double decimal_num=num;
  4.     cout << num << '\t' decimal_num << endl;
  5.     double decimal_den=den;
  6.     cout << den << '\t' << decimal_num << endl;
  7.     double decimal=(decimal_num)/(decimal_den);
  8.     cout << decimal << endl;
  9.     return decimal;
  10. }

Hello,

disregard my last message.

display shows
3 3
4 4
after entering 3/4 fraction answeer = 0
Apr 3 '07 #9
Ganon11
3,652 Expert 2GB
When you are assigning decimal_num and decimal_den, try

Expand|Select|Wrap|Line Numbers
  1. decimal_num = static_cast<double>(num);
  2. decimal_den = static_cast<double>(den);
to explicitly cast the ints to doubles.
Apr 3 '07 #10
Coldfire
289 100+
while doing this
double decimal=(decimal_num)/(decimal_den);
"it is possible for a value like 0.123 to be converted to 0 depending upon the particular compiler"

In your case the double decimal value loses the imaginary part of the result. So, you need to store this result by typecasting it or using routines like atof(). Try using float. I dont have the compiler to test this simple thingy rightnow but try the above recommendations
Apr 3 '07 #11
When you are assigning decimal_num and decimal_den, try

Expand|Select|Wrap|Line Numbers
  1. decimal_num = static_cast<double>(num);
  2. decimal_den = static_cast<double>(den);
to explicitly cast the ints to doubles.

Hello,

using static_cast did not work. The function is part of a fraction class I made. Here is how I call it in the main:

Expand|Select|Wrap|Line Numbers
  1. case 'D': get_fraction(f1);
  2. f1.decimal_value();
  3. cout<<"the Decimal value is ==> " << f1 << endl; 
  4. break;
the get_fraction() is a local function to the main.

f1 is a loacal variable of type fraction
Apr 3 '07 #12
sicarie
4,677 Expert Mod 4TB
Hello,

using static_cast did not work. The function is part of a fraction class I made. Here is how I call it in the main:

Expand|Select|Wrap|Line Numbers
  1. case 'D': get_fraction(f1);
  2. f1.decimal_value();
  3. cout<<"the Decimal value is ==> " << f1 << endl; 
  4. break;
the get_fraction() is a local function to the main.

f1 is a loacal variable of type int
f1 isn't a user-defined container of some sort (class, struct,...)? That would definitely be my guess as to why the precision is disappearing, but even then, you have some interesting implementations in this program...
Apr 3 '07 #13
Coldfire
289 100+
Hello,

disregard my last message.

display shows
3 3
4 4
after entering 3/4 fraction answeer = 0
Problem Solved ...........you just need to do this
double decimal=(double)(decimal_num)/(decimal_den);
or
double decimal=(float)(decimal_num)/(decimal_den);
Apr 3 '07 #14
sicarie
4,677 Expert Mod 4TB
Problem Solved ...........you just need to do this
double decimal=(double)(decimal_num)/(decimal_den);
or
double decimal=(float)(decimal_num)/(decimal_den);
And already a Problem Solver... nice!
Apr 3 '07 #15
Coldfire
289 100+
And already a Problem Solver... nice!
:D

_____________________________

P.S. I am so sick of this message: The message you have entered is too short. Please lengthen your message to at least 10 characters.
Apr 3 '07 #16
sicarie
4,677 Expert Mod 4TB
:D

_____________________________

P.S. I am so sick of this message: The message you have entered is too short. Please lengthen your message to at least 10 characters.
Yeah, one of the mods in the javascript forum found a way to bypass it, but I never got a chance to ask him how...
Apr 3 '07 #17
Problem Solved ...........you just need to do this
double decimal=(double)(decimal_num)/(decimal_den);
or
double decimal=(float)(decimal_num)/(decimal_den);
Okay tried it. Now the return is the fraction I enter.

example:

3/4
the decimal value is 3/4.


Expand|Select|Wrap|Line Numbers
  1. float fraction::decimal_value()
  2.     {
  3.         float decimal_num=num;
  4.         cout<< num <<decimal_num<<endl;
  5.         float decimal_den=den;
  6.         cout<< den <<decimal_den <<endl;
  7.         float decimal=(float)(decimal_num)/(decimal_den);
  8.         return decimal;
  9.     }
Apr 3 '07 #18
Coldfire
289 100+
Okay tried it. Now the return is the fraction I enter.

example:

3/4
the decimal value is 3/4.
u mean the required answer ...in your case = 0.75 !
Apr 4 '07 #19
u mean the required answer ...in your case = 0.75 !
yes, that is correct. I have it working now. It is amazing that something so small can take so long make work when you are a newbie to c++

Thanks for the help
Apr 4 '07 #20

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

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
10
by: David Ricker | last post by:
I am having problems adding two numbers. I am trying to add 1.005 and 1.007 to come up with 2.012. Should be easy enough right? Problem is that I keep getting 2.0119999999999996 as my result. ...
5
by: Trevor Braun | last post by:
Hi, I'm not sure that this is the right forum for this, but I've been having a very tough time completing this expression, and I was hoping someone might have some suggestions for me. I am trying...
9
by: arun.hallan | last post by:
I need to derive fractions from decimals as so: 0.3333 = 1/3 0.6666 = 2/3 The decimal could also be 0.33, or 0.3333333 but the point is that it that the fraction needs to be extracted. ...
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
2
by: Anan18 | last post by:
Use Integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is...
1
by: sony.m.2007 | last post by:
Hi, I have a decimal value and i need to take the digits after decimal places(fraction part only). Is there any simple functions available other than using string functions(searching for '.' and...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.