Public Group

Short Question

This topic is 5024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Is there any way to tell if there is nothing in the decimal place of a float? For example, is there anyway to write a program that will multiply, say, .2953 by ten until it is 2953?

Share on other sites
If you need to do something like this you shouldn't use floats.

Hint: What is the correct result for 0.1?

Share on other sites
The computer has no concept of ".2953". It cannot comprehend the infinite, and it can no more write ".2953" with a finite number of bits than you could write "one third" with a finite number of decimal digits. Thus, having nothing in "the decimal place of a float" is meaningless, because a float doesn't have a decimal point, it has a, well, binary point.

Share on other sites
To clarify, modern contemporary PCs store floating-point numbers according to the IEEE Std 754-1985 IEEE Standard for Binary Floating-Point Arithmetic, which specifies the usage of a certain number of bits in a 32-bit word as a mantissa, with the rest, save a sign bit, serving as the exponent. This means that a number like 0.1 is not stored in whole and fractional parts, but rather as an expressions xy that approximates the value 0.1. For this reason, your question is malformed.

Share on other sites
Quote:
 Original post by DakarIs there any way to tell if there is nothing in the decimal place of a float? For example, is there anyway to write a program that will multiply, say, .2953 by ten until it is 2953?

Yes, but you would have to jump through a few hoops. Take for example something like this.

#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char* argv[]){	char buffer[256];		double value = 25.12345;	// Convert to a string	sprintf( buffer,"%f",value);	// Display the string	printf("Old Value: %s\n",buffer);	// The new value	float newvalue = value;	// Flag to tell when we should multily	bool mult = 0;        // Now we convert the float into an int	for( int x = 0;x < strlen(buffer);x++)	{		if( buffer[x] == '.' )			mult = 1;		else if( mult )			newvalue *= 10;	}        // Now we need to truncate off all trailing zeros that are not needed	for( x = strlen(buffer) - 1; x > -1 ;x--)	{		if( buffer[x] == '0' ) 			newvalue /= 10;		else			break;	}	// Display the string	printf("New Value: %f\n",newvalue);	return 0;}

All you have to do is convert to a string then manipulate it to how you want it. You can determine what's in front of the decimal by taking a substring from the start of the string to the decimal point. In that example, I made the data type a double since floats will round the ends. I think that is what you are trying to do, but I do not know. I know there are probabally better ways of doing it using C++ code, but this is just a proof of concept that it is possible by using other data types. You will probabally want to convert the final answer to an int to make sure the trailing 0's are also removed from the float resultant if it looks ugly.

Here are the results:
Old Value: 25.123450New Value: 2512345.000000Press any key to continue

- Drew

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• Forum Statistics

• Total Topics
633288
• Total Posts
3011222
• Who's Online (See full list)

There are no registered users currently online

×