15,902,299 members
Home / Discussions / C / C++ / MFC

# C / C++ / MFC

 Re: Calculating decimal places - ptr_Electron16-Oct-18 2:52 ptr_Electron 16-Oct-18 2:52
 Re: Calculating decimal places - ptr_Electron16-Oct-18 3:03 ptr_Electron 16-Oct-18 3:03
 Re: Calculating decimal places - David Crow16-Oct-18 3:06 David Crow 16-Oct-18 3:06
 Re: Calculating decimal places - ptr_Electron16-Oct-18 3:14 ptr_Electron 16-Oct-18 3:14
 Re: Calculating decimal places - David Crow16-Oct-18 3:20 David Crow 16-Oct-18 3:20
 Re: Calculating decimal places - ptr_Electron16-Oct-18 3:29 ptr_Electron 16-Oct-18 3:29
 Re: Calculating decimal places - David Crow16-Oct-18 3:38 David Crow 16-Oct-18 3:38
 Re: Calculating decimal places - leon de boer16-Oct-18 15:28 leon de boer 16-Oct-18 15: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 veritasmodified 17-Oct-18 10:51am.
 Re: Calculating decimal places - CPallini16-Oct-18 2:48 CPallini 16-Oct-18 2:48
 Re: Calculating decimal places - ptr_Electron16-Oct-18 2:55 ptr_Electron 16-Oct-18 2:55
 Re: Calculating decimal places - CPallini16-Oct-18 3:11 CPallini 16-Oct-18 3:11
 Using assignment operator = for a class with const memeber variables? Member 140109547-Oct-18 12:52 Member 14010954 7-Oct-18 12:52
 Re: Using assignment operator = for a class with const memeber variables? CPallini7-Oct-18 20:36 CPallini 7-Oct-18 20:36
 Re: Using assignment operator = for a class with const memeber variables? Richard MacCutchan7-Oct-18 21:47 Richard MacCutchan 7-Oct-18 21:47
 Re: Using assignment operator = for a class with const memeber variables? «_Superman_»9-Oct-18 1:51 «_Superman_» 9-Oct-18 1:51
 Access a pointer value changed in another class meerokh5-Oct-18 6:30 meerokh 5-Oct-18 6:30
 Re: Access a pointer value changed in another class Victor Nijegorodov5-Oct-18 7:51 Victor Nijegorodov 5-Oct-18 7:51
 Re: Access a pointer value changed in another class meerokh5-Oct-18 8:10 meerokh 5-Oct-18 8:10
 Re: Access a pointer value changed in another class Victor Nijegorodov5-Oct-18 8:37 Victor Nijegorodov 5-Oct-18 8:37
 Re: Access a pointer value changed in another class CPallini5-Oct-18 11:28 CPallini 5-Oct-18 11:28
 Message Closed 9-Oct-18 23:31 Henry John 9-Oct-18 23:31
 Re: Access a pointer value changed in another class CPallini9-Oct-18 23:58 CPallini 9-Oct-18 23:58
 Conversion from C to C#, Need C# Method Signature akvbsoft4-Oct-18 8:50 akvbsoft 4-Oct-18 8:50
 Re: Conversion from C to C#, Need C# Method Signature vcbeaut4-Oct-18 16:48 vcbeaut 4-Oct-18 16:48
 Re: Conversion from C to C#, Need C# Method Signature Victor Nijegorodov4-Oct-18 21:43 Victor Nijegorodov 4-Oct-18 21:43
 Last Visit: 31-Dec-99 18:00     Last Update: 20-May-24 20:50 Refresh ᐊ Prev1...255256257258259260261262263264 Next ᐅ