Sign in to follow this  

Short Question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Dakar
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?


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.123450
New Value: 2512345.000000
Press any key to continue


- Drew

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this