Click here to Skip to main content
14,366,827 members

C / C++ / MFC

 
GeneralRe: Calculating decimal places - Pin
ptr_Electron16-Oct-18 3:52
memberptr_Electron16-Oct-18 3:52 
GeneralRe: Calculating decimal places - Pin
ptr_Electron16-Oct-18 4:03
memberptr_Electron16-Oct-18 4:03 
GeneralRe: Calculating decimal places - Pin
David Crow16-Oct-18 4:06
memberDavid Crow16-Oct-18 4:06 
GeneralRe: Calculating decimal places - Pin
ptr_Electron16-Oct-18 4:14
memberptr_Electron16-Oct-18 4:14 
QuestionRe: Calculating decimal places - Pin
David Crow16-Oct-18 4:20
memberDavid Crow16-Oct-18 4:20 
AnswerRe: Calculating decimal places - Pin
ptr_Electron16-Oct-18 4:29
memberptr_Electron16-Oct-18 4:29 
GeneralRe: Calculating decimal places - Pin
David Crow16-Oct-18 4:38
memberDavid Crow16-Oct-18 4:38 
GeneralRe: Calculating decimal places - Pin
leon de boer16-Oct-18 16:28
memberleon de boer16-Oct-18 16:28 
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


modified 17-Oct-18 10:51am.

QuestionRe: Calculating decimal places - Pin
CPallini16-Oct-18 3:48
mveCPallini16-Oct-18 3:48 
AnswerRe: Calculating decimal places - Pin
ptr_Electron16-Oct-18 3:55
memberptr_Electron16-Oct-18 3:55 
GeneralRe: Calculating decimal places - Pin
CPallini16-Oct-18 4:11
mveCPallini16-Oct-18 4:11 
QuestionUsing assignment operator = for a class with const memeber variables? Pin
Member 140109547-Oct-18 13:52
memberMember 140109547-Oct-18 13:52 
AnswerRe: Using assignment operator = for a class with const memeber variables? Pin
CPallini7-Oct-18 21:36
mveCPallini7-Oct-18 21:36 
AnswerRe: Using assignment operator = for a class with const memeber variables? Pin
Richard MacCutchan7-Oct-18 22:47
protectorRichard MacCutchan7-Oct-18 22:47 
AnswerRe: Using assignment operator = for a class with const memeber variables? Pin
«_Superman_»9-Oct-18 2:51
professional«_Superman_»9-Oct-18 2:51 
QuestionAccess a pointer value changed in another class Pin
meerokh5-Oct-18 7:30
membermeerokh5-Oct-18 7:30 
AnswerRe: Access a pointer value changed in another class Pin
Victor Nijegorodov5-Oct-18 8:51
memberVictor Nijegorodov5-Oct-18 8:51 
GeneralRe: Access a pointer value changed in another class Pin
meerokh5-Oct-18 9:10
membermeerokh5-Oct-18 9:10 
GeneralRe: Access a pointer value changed in another class Pin
Victor Nijegorodov5-Oct-18 9:37
memberVictor Nijegorodov5-Oct-18 9:37 
AnswerRe: Access a pointer value changed in another class Pin
CPallini5-Oct-18 12:28
mveCPallini5-Oct-18 12:28 
QuestionRe: Access a pointer value changed in another class Pin
CPallini10-Oct-18 0:58
mveCPallini10-Oct-18 0:58 
QuestionConversion from C to C#, Need C# Method Signature Pin
akvbsoft4-Oct-18 9:50
memberakvbsoft4-Oct-18 9:50 
AnswerRe: Conversion from C to C#, Need C# Method Signature Pin
vcbeaut4-Oct-18 17:48
membervcbeaut4-Oct-18 17:48 
AnswerRe: Conversion from C to C#, Need C# Method Signature Pin
Victor Nijegorodov4-Oct-18 22:43
memberVictor Nijegorodov4-Oct-18 22:43 
QuestionMake screen bigger Pin
Member 139824711-Oct-18 9:37
memberMember 139824711-Oct-18 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.