Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Input: 1.0011
Output: Valid
---------------------
Input: 20.1001
Output: Invalid
------------------------
* Floating point number should contain only zeros and ones and a point(.)
* I have to do some calculation with that number, so if you convert it to char array then please convert it to float number at the end.

What I have tried:

I can get integer part easily by type casting then do modulo operating to separate each number and check 1 or 0, but can not get those number after point(.)
I think there will be a better solution of this.
This question can also be stated as, wap to check a valid binary floating point number is or not.
All I have to achieve is to check if the number contains only 1,0 and point(.) or not.
Posted
Updated 9-Mar-16 1:52am

So stop converting it to a number and start by validating the string.
Create a bool value "hasPoint", and set it to false.
loop through the input string, examining each character in turn.
If it is a '0' or a '1', then it's fine.
if it's a '.', then check "hasPoint" if it's false, then set it to true, and all is fine.
if it's fine, loop round to look at the next character.
Otherwise, it's an invalid character and you should print an error message and stop.
After the loop, you can use the standard floating point routines to convert teh value, because you know it it valid. (You could do it as part of the loop, but that's more complex because the decimal changes "where" you need to insert the value).
 
Share this answer
 
Comments
Rupam Bhaduri Pro 9-Mar-16 7:39am    
@OriginalGriff, I thought that but I don't know how to convert the string into its equivalent float number, for say, char ch[] = 11010.011; to float a = 11010.011; will type casting work here like float b = float(ch); ?
OriginalGriff 9-Mar-16 8:25am    
Use the atof function:
http://www.tutorialspoint.com/c_standard_library/c_function_atof.htm
The simplest solution would be checking the string (the characters) when the number is entered (as stated in the question):
C++
bool bResult = (*input != 0);
int decPoint = 0;
while (bResult && *input)
{
    switch (*input)
    {
    case '.' : decPoint++;
    case '0' :
    case '1' : break;
    default : bResult = false;
    }
    input++;
}
if (decPoint != 1)
    bResult = false;


Converting the input to a floating point value will not work because those can not represent real number in most cases.

For example the value 1.0011 is represented internally as 0x3F80240B with single precision upon conversion (e.g. when using atof()). Printing this out with full precision it will be 1.00109994.

For double precision it will be 0x3FF004816F0068DC which is 1.0011000000000001.

You mentioned that you are already able to check the integer part. This can be also used for the fractional part by just treating that as integer too:
Locate the decimal point using strchr(), pass the returned pointer plus one to the conversion function atoi() and perform the same check as for the integer part.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900