Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi all,
In my program, there is an edit box, where the user can type in the values. This value is used in some functions. But as soon as I close my s/w and reopen, this value is rounded off.
Even though the actual values typed is taken for calculations, this rounding off of values is kind of misleading/confusing the user.
How can I get rid of this?
 
The code for the edit button:
  InsCtrl(80, EC_EDIT,m_userENZ1.x);
//m_userENZ1 is CV3 type.
// EC_EDIT is declared in a header file as :- const int EC_EDIT = 0x000040;
If the user enters a value:
560988.92 it is rounded off to: 560989
6767202.49 it is rounded off to: 6.7672e+006
Though these values do not affect the calculations,I need to get rid of this.
Need some help please..
Posted 1-Aug-12 5:17am
v2
Comments
Richard MacCutchan at 1-Aug-12 12:02pm
   
How are you displaying these values in the edit control?
Member 8446342 at 2-Aug-12 4:28am
   
I didn't get you.. it is displayed using :
InsCtrl(80, EC_EDIT,m_userENZ1.x);
 
where InsCtrl is a method in the class - CECtrl
InsCtrl(int c, int type, double& val, CEFlex* pFlex)
Richard MacCutchan at 2-Aug-12 5:15am
   
And how does this class format its numbers to display them?
Member 8446342 at 2-Aug-12 5:22am
   
In this particular class, there is a settings function, where the object is added:-
obj.Item(_T("Source_Easting"), m_srcENZFx.x, 10);
There is another function in another main class where this function lies-
 
void CESetObj::Item(const CString& itm, double& val, int dp)
{ CString str, fmt;
fmt.Format(_T("%%.%dlf"), dp);
Add(itm, fmt, val)
}.
Richard MacCutchan at 2-Aug-12 5:33am
   
Member 8446342 at 2-Aug-12 6:56am
   
These programs are pre written/defined and I cannot alter anything in there. But yes I did have a look at the link you've sent, but don't quite understand what is wrong.
Richard MacCutchan at 2-Aug-12 8:21am
   
The format string is %%.%dlf which is meaningless. I suggest you go to whoever wrote this class and get them to fix it.
Member 8446342 at 2-Aug-12 8:52am
   
Alright..
Philip Stuyck at 1-Aug-12 13:23pm
   
what is a CV3 ?
I have the feeling the problem is caused by using a float or a double. This kind of variable is not able to store all values exactly but something close to the desired value. This can sometimes be a problem.
Member 8446342 at 2-Aug-12 4:21am
   
CV3 in my program is a class with one of its method- CV3(double x, double y, double z).This is mainly used in calculations involving latitude, longitude and height.(3 parameters)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It seems you are storing the value that is typed into the edit control as a float/double.
 
There is always a potential loss of precision and/or change of representation if you convert from a string to a float/double and the convert back.
 
If you want to preserve exactly what's typed, then you need to store the value of the edit control as a string and save and restore that string -- then only convert it to a float/double when you do your calculation.
  Permalink  
Comments
Kenneth Haugland at 1-Aug-12 18:20pm
   
GIve you a 5 for this one
pasztorpisti at 1-Aug-12 18:41pm
   

float f = 560988.92f;
printf("%f\n", f);

This code prints 560988.937500 on my machine. As the integer part grows, the fractions become less and less accurate as the answer says but we still have some accuracy in this example. The example with 6767202.49 gives very strange result for the asker, even the integer part is rounded in his example...
TRK3 at 1-Aug-12 18:53pm
   
What's your point?
 
560988.92 = 560988 to 6 digits.
 
6767202.49 = 6767200 = 6.7672e6 to 6 digits.
 
We don't know what actual data type a CV3 is, but we can safely conclude that convert between a string and CV3 representation preserves approximatesly 6 digits of accuracy.
 
For all we know it's an 8 byte BCD encoded value with 2 digits of exponent and 6 digits of significand.
 
Or it's a 24 bit floating point number with 8 bits of exponent and 16 bits of significand.
pasztorpisti at 1-Aug-12 19:56pm
   
My bad! I was thinking in float! :-)
Then this problem is solved. +5
Member 8446342 at 2-Aug-12 4:31am
   
CV3 in my program is a class with one of its method- CV3(double x, double y, double z).This is mainly used in calculations involving latitude, longitude and height.(3 parameters)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Instead of directly declaring the variable in the edit box, it is called from the main edit class;Also since the EC_EDIT is a const int variable, I explicitly declared the number of decimal places.
 CEEdit*  east;
 east = (CEEdit*)InsCtrl(157, EC_EDIT, m_userENZ1.x);
 east->decm = 3;
 
The decm is an int variable in CEEdit class, which is used for the number of decimal places. Here, defining the number of places as 3, it retains 3 decimal places, without rounding off.
 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 454
1 Maciej Los 404
2 CPallini 185
3 Sergey Alexandrovich Kryukov 182
4 Richard MacCutchan 120
0 OriginalGriff 5,634
1 Sergey Alexandrovich Kryukov 4,763
2 Peter Leow 2,954
3 Maciej Los 2,614
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web02 | 2.8.140718.1 | Last Updated 2 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid