Calculating decimal places -
-
Hi Below code does some memory corruption due to which access violation happens at sub subsequent steps If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help
while ((int)tax % 10 != 0)
{
tax *= 10;
decimals++;
}decimalpoints = decimals-1
-
Hi Below code does some memory corruption due to which access violation happens at sub subsequent steps If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help
while ((int)tax % 10 != 0)
{
tax *= 10;
decimals++;
}decimalpoints = decimals-1
Both are not pointers. one issue I found with this is the loop iterates for 19 times for the value of tax = 8.995. by end of the loop decimal = 19. I except decimal = 3 by end of the loop
-
Hi Below code does some memory corruption due to which access violation happens at sub subsequent steps If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help
while ((int)tax % 10 != 0)
{
tax *= 10;
decimals++;
}decimalpoints = decimals-1
This is not going to work as you might expect. For example, if
tax
has an initial value of8.0
, it may actually be stored in memory as8.00000000001
or something similar. What exactly are you trying to accomplish? A better solution may be just an explanation away."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Hi Below code does some memory corruption due to which access violation happens at sub subsequent steps If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help
while ((int)tax % 10 != 0)
{
tax *= 10;
decimals++;
}decimalpoints = decimals-1
-
This is not going to work as you might expect. For example, if
tax
has an initial value of8.0
, it may actually be stored in memory as8.00000000001
or something similar. What exactly are you trying to accomplish? A better solution may be just an explanation away."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3
-
Tax variable is of type Double
-
This is not going to work as you might expect. For example, if
tax
has an initial value of8.0
, it may actually be stored in memory as8.00000000001
or something similar. What exactly are you trying to accomplish? A better solution may be just an explanation away."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3
-
I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3
ptr_Electron wrote:
for example 8.995 is the tax amount.
Which could be stored in memory as
8.9949999999999992
or something similar. Then you would get16
(or maybe15
, I forget) as an answer."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Tax variable is of type Double
-
ptr_Electron wrote:
for example 8.995 is the tax amount.
Which could be stored in memory as
8.9949999999999992
or something similar. Then you would get16
(or maybe15
, I forget) as an answer."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
May I know the correct approach please.
-
May I know the correct approach please.
It's still unclear as to what you are trying to accomplish. Once you have the number of digits in the mantissa, then what? Again, if you would provide details for your end-game, we may be able to offer a better solution. Manipulating floating point numbers on a binary computer is not a trivial task.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
It's still unclear as to what you are trying to accomplish. Once you have the number of digits in the mantissa, then what? Again, if you would provide details for your end-game, we may be able to offer a better solution. Manipulating floating point numbers on a binary computer is not a trivial task.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Thanks for response. a given decimal number. I would the count of decimal points. For example 6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5 Count of digits after decimal point
-
Thanks for response. a given decimal number. I would the count of decimal points. For example 6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5 Count of digits after decimal point
ptr_Electron wrote:
6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5
Which are stored in memory as:
6.6699999999999999
6.9875999999999996
1.6789000000000001or something very similar. Notice all the mantissas are the same length? Is this just a trivial exercise for you, or are you ultimately planning on doing something with the result?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Both are not pointers. one issue I found with this is the loop iterates for 19 times for the value of tax = 8.995. by end of the loop decimal = 19. I except decimal = 3 by end of the loop
-
Thanks for response. a given decimal number. I would the count of decimal points. For example 6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5 Count of digits after decimal point
As per what David has explained all doubles are actually stored subtly different to what you think because they round. I just want to extend why they round. They round because the computer works in base 2 (0 and 1's) and you are working in decimal base 10 (0,1,2,..9) 10 does not work as a power of 2 you can go either side 2x2x2=8 OR 2x2x2x2=16 so any base 10 decimal fraction when written in base 2 will likely round Double-precision floating-point format - Wikipedia[^] Assuming you are on a standard Microsoft compiler many will be rounded at 52 bits long and we have no way to know what length you actually typed in. So usually when writing doubles to screen you fix the decimal places Here is how the standard print function does it the %.3f means take float write to 3 decimal places
printf("Double value: %.3f\n", 3.1234543747321475);
I have made a randomly long value but if you execute it only puts out 3 decimal places. There are many conversion routines in C/C++ to convert them in fixed decimal places to screen, buffers etc. So generally you fix the length at display or while the number is in string format, you can't work the problem in reverse the moment it stores the original length is lost. So basically once stored there is no way to count the decimal places .. you can't do what you asked. Lastly should add this is nothing to do with C, any language that stores numbers as doubles behaves that way.
In vino veritas