473,403 Members | 2,293 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,403 software developers and data experts.

Suggestion for a function for checking fraction number

I have written a function to check if the number is fraction in either X/X or X X/X format where X is double.

Here is the code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. bool IsFraction(const CComBSTR bucketSize, double &result)
  3. {    
  4.     int nToken=1;
  5.     HRESULT hr = S_OK;
  6.     CComBSTR tempBucketSize(bucketSize);
  7.     //Count the token. must be 2 or 3
  8.     wcstok(tempBucketSize.m_str, _T(" /"));
  9.     while(wcstok(NULL, _T(" /")))
  10.     {
  11.         ++nToken;
  12.     }
  13.  
  14.     //If number token == 2, the number must be in the form X/X
  15.     if(nToken == 2)
  16.     {
  17.         CComBSTR szToken = wcstok(bucketSize.m_str, _T("/"));
  18.  
  19.         CComVariant vtNumerator = szToken;
  20.         hr = vtNumerator.ChangeType( VT_R8 );
  21.         //if szToken is not number return false
  22.         if(FAILED(hr) || (vtNumerator.vt != VT_R8) || (vtNumerator.dblVal < 0.0))
  23.             return false;
  24.  
  25.         //if there's no string left or the second string is not number or it's zero, return false
  26.         szToken = wcstok(NULL, _T("/"));
  27.         CComVariant vtDenominator = szToken;
  28.         hr = vtDenominator.ChangeType(VT_R8);
  29.  
  30.         if(FAILED(hr) || (vtDenominator.vt != VT_R8) || (vtDenominator.dblVal <= 0.0))
  31.             return false;
  32.  
  33.         //otherwise calculate number and return true
  34.         result = vtNumerator.dblVal / vtDenominator.dblVal;
  35.         return true;
  36.     }
  37.     //If number token == 3, the number must be in the form X X/X
  38.     else if(nToken == 3)
  39.     {
  40.         CComBSTR szToken = wcstok(bucketSize.m_str, _T(" "));
  41.  
  42.         CComVariant vtNumber = szToken;
  43.         hr = vtNumber.ChangeType(VT_R8);
  44.         //if szToken is not number return false
  45.         if(FAILED(hr) || (vtNumber.vt != VT_R8) || (vtNumber.dblVal < 0.0))
  46.             return false;
  47.  
  48.         //if there's no string left or the second string is not number, return false
  49.         szToken = wcstok(NULL, _T("/"));
  50.         CComVariant vtNumerator = szToken;
  51.         hr = vtNumerator.ChangeType(VT_R8);
  52.  
  53.         if(FAILED(hr) || (vtNumerator.vt != VT_R8) || (vtNumerator.dblVal < 0.0))
  54.             return false;
  55.  
  56.         //if there's no string left or the third string is not number or it's zero, return false
  57.         szToken = wcstok(NULL, _T(" /"));
  58.         CComVariant vtDenominator = szToken;
  59.         hr = vtDenominator.ChangeType(VT_R8);
  60.  
  61.         if(FAILED(hr) || (vtDenominator.vt != VT_R8) || (vtDenominator.dblVal <= 0.0))
  62.             return false;
  63.  
  64.         //otherwise calculate number and return true
  65.         result = vtNumber.dblVal + vtNumerator.dblVal / vtDenominator.dblVal;
  66.         return true;
  67.  
  68.     }
  69.     else
  70.         return false;
  71. }
  72.  
It's very long but I can't think of another algorithm to deal with it. So if you have an idea to optimize it, please give me your suggestions or comments.
Thank
Aug 20 '10 #1
5 2917
newb16
687 512MB
Is 1.25 3.67/0.87 a fraction too, like 1 1/4 is ? If yes then ok, without decreasing number of checks it probably can't be reduced. What is VT_R8, btw?
Aug 20 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
1 1/4 is not a fraction. It's a mixed number. The actual number is 5/4 which is a rational number.

1.25 3.67/0.87 is incorrect syntax. The 3.67/0.87 is a real number.

Note that 5/4 maps to 5.0/4.0 making the rational numbers a subset of the real numbers.

If you use the form X/X where X is a floating point number, you will have a correct representation.
Aug 20 '10 #3
donbock
2,426 Expert 2GB
kouisawang ...

What precise forms do you need to recognize as fractions?
Expand|Select|Wrap|Line Numbers
  1. 100          whole number (implied denominator of 1)
  2. 1/4          ratio of integers
  3. 0.45         decimal fraction (denominator implied by number of digits)
  4. 2.67         mixed number (decimal fraction)
  5. 2 1/2        mixed number (ratio, whitespace separator)
  6. 2-1/2        mixed number (ratio, dash separator)
  7. 1.5/7.8      ratio of real numbers
  8. 1.2 1.5/7.8  funky mixed number
  9. 4,5          European decimal point
How many ways do you need to support negative numbers?
-100, -1/4, 1/-4, -2 1/2, -(2 1/2), etc
Aug 20 '10 #4
My intention is to have X/X and X X/X as a valid input format. X is double. however, it only supports positive number. Maybe using "fraction" is not right coz I do count numerator number with decimal point as valid input but ... you get my intention.
btw, vt_r8 is a double variant type
Aug 22 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
You might consider a VT_I4 or a VT_UI4 rather than a
VT_R8 for your VARIANT.

You can always create a X X/X from a X/X and you can convert an input X X/X to an X/X. So the common format X/X seems you best bet.

Further, numerators and denominators are integers and not reals. Hcne, I recommend the VT_I4 or for positives only, a VT_UI4.
Aug 22 '10 #6

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

Similar topics

2
by: EricLin | last post by:
Dear all: I am just concerned about how to use a function with variable number of arguments. for example: int TestFunction(arguments) { .... Testing contents... }
26
by: G Patel | last post by:
Hi, I'm wondering if anyone knows if the following function will function properly as a set-bit counter on non 2s complement machines (as K&R2 implies). | int bitcount(unsigned x) | { | ...
3
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
3
by: pocmatos | last post by:
Hi all, I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits + and keep them in an int, however if the number is bigger than int, I should output error....
7
by: Richard Hollenbeck | last post by:
How can a numeric field display results in fractions? If my recipe data requires 1/2 cup of something, for example, I'm willing to punch in 0.5 in the quantity field then select cup in the unit...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
sonic
by: sonic | last post by:
I am fairly new to C++. I was wondering how would one go about checking to see if a value entered by the user was negative or positive. Is this something you might use modulo(%) math for? ...
1
by: eadavid | last post by:
HI, I am a beginner to perl and I need to write perl code for checking a good die in the wafermap. Here is the wafermap look like in file ....XXXX111111111X1111XXXX.......
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.