|
A floating point value is never precise. Sorry, that's how it goes. Try including math.h and do this:
int tempint = (int )ceil(
tempflt*100);<br />
<br />
I think that's the right syntax, but the Math class has floor and ceiling methods, which round up and down. This will round your 379.xxx up to 380.00, then you cast it to int.<br />
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Christian Graus wrote: A floating point value is never precise.
How predictable is a floats actual value? Because I don't think ceil will be a general solution. What about cases when I should be rounding down?
Is double more precise? I really don't need too much precision as far as decimal points goes. The range I will be in will be -32.768 to 32.767 (the range of short divided by 1000), so I only need 3 numbers after the decimal, which is why i chose float instead of double.
-- modified at 19:35 Tuesday 16th January, 2007
|
|
|
|
|
acerunner316 wrote: What about cases when I should be rounding down?
floor
acerunner316 wrote: Is double more precise?
Yes, but not exact.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Christian Graus wrote: acerunner316 wrote:
What about cases when I should be rounding down?
floor
That's my point. I need a generic code that will accept a float and multiply by 100 and get the right integer. Without knowing how float will skew the result of the multiply operation, I won't know when ot use ceil, and when to use floor.
|
|
|
|
|
you always would need ceil. Just like adding .5 assumes that you need to round up.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
This is because the float (or double) to int cast performs a truncated conversion. The actual answer was probably more like 379.9999999999999999 but the truncation gives a result of 379.
If you want a rounded answer then do this :
int tempint = (int)( tempflt * 100 + 0.5 );
|
|
|
|
|
That's a good idea. As long as the float math operations don't skew the answers by more than 0.5, which it doesn't seem like it will do.
|
|
|
|
|
They won't, that always guarentees you'll get what you want. ( I forgot that little trick )
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
The best method of making sure you get the correct value is to add 0.5 before casting.
|
|
|
|
|
Hi All,
I have a couple fo functions which are not behaving. IsInteractiveUser() always returns TRUE, even when one T/S's into a Server. The opposite is true for IsTerminalUser() - it always returns FALSE.
I know I am building the well known SIDs correctly: Interactive is S-1-5-4 (SECURITY_INTERACTIVE_RID), Terminal Server is S-1-5-13 (SECURITY_TERMINAL_SERVER_RID). Both are being contstructed using NT Authority S-1-5 (SECURITY_NT_AUTHORITY). See Well Known SIDs[^]
I've examined both TOKEN_USER and TOKEN_GROUPS from GetTokenInformation(). Is there another place which one should look? I can't imagine there is another authority which should be used.
Similar functions IsNTAuthority() (using SECURITY_LOCAL_SYSTEM_RID) and IsNTService() (using SECURITY_SERVICE_RID) work fine...
Any ideas?
Jeff
|
|
|
|
|
I am an experienced developer, but completely new to Windows MFC and Visual C++. One part of my application allows the user to edit a file that is displayed in hex - 16 bytes/line. A typical user interface for this sort of thing allows the user to select (left button down) and highlight the element to change in the view. The editing then occurs in place. I can handle the selection and highlight part of object selection using bounding rectangles, but I am struggling with the "edit" portion of the task. I am guessing that this is done with some sort of modeless dialogue, but I am really clueless. A code snippet would be great, but even a guideline about the general approach would be helpful.
Address | 0 | 1 | 2 | 3 | 4 ....... | E | F |
------- ---------------------------------------
0x0000 FF 2C 31 FF ............ FF
0x0010 A0 29 13 47 ............ 55
Thanks, Stan
|
|
|
|
|
Generally you create a textbox as a child control, position it over the label and make it visible. When focus leaves the textbox, or it gets an enter press, your code pulls out the value, uses it, and hides the control, which is generally a member variable.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Thanks - The textbox sounds like just the ticket.
I still have a few questions.
1) My work so far uses C++ MFC. Must I (should I?) use CLR to get the textbox functionality?
Stan
|
|
|
|
|
*grin* that's one question.
No, if you did it using CLR, you'd just be adding CLR, at the core, your code would eventually do the same thing, and use the same control. MFC libraries do this sort of thing all the time.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
OK, Here is more than one:
0) I assume that this "textbox task" is strictly a programming task (as opposed to using resource editor for example). Is this correct?
1) If the answer to (0) is yes, what is the base class to start with for the textbox implementation? CEdit? Is there a derived class that is more suitable?
2) Can you point me at some sample code that uses a textbox for some similar task? If not, ...
3) Can you provide me with some "thread starters" that I can investigate in the Visual C++ index to MFC. I have looked under "textbox" and "control".
4) References I have:
a) Ivor Horton - "Beginning Visual C++ 2005"
b) Mike Blaszczak - "MFC with Visual C++ 5" (1997!)
Can you recommend any additional or better references?
Thanks for your help,
Stan
P.S. I don't know what the title "MVP" means, but I have chosen to translate it to "Most Valuable Person".
|
|
|
|
|
0 - yes
1 - I'd use CEdit. I'm not aware of any derived classes, actually
2 - If you google for samples that allow editing of an MSFlexGrid, or Chris has a grid for download on the site. The former will always use this method, the grid may, too.
3 - MSFlexGrid is a good googling point, at one stage there were lots of samples for doing this to that control.
4 - I recommend the MFC Answer Book, and perhaps MFC Unleashed.
MVP is a Microsoft award, it means Most Valued Professional, I think. CP does the same thing, but I don't get an MVP icon here, because I already have a staff icon
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
When your editing window gets focus you can go into a modal loop and process keystrokes and any
other user-input messages you need to handle. You can use the Caret[^] functions to handle the
insertion caret. Using a monospaced font makes this MUCH easier.
Here's an example shell for the message loop...
MSG Msg;
bool fEditing = true;
while (fEditing)
{
if (::PeekMessage(&Msg,0,0,0,PM_REMOVE))
{
::TranslateMessage(&Msg);
::DispatchMessage(&Msg);
if (Msg.hwnd != hMyWnd)
continue;
if (WM_KEYDOWN == Msg.message)
{
switch (Msg.wParam)
{
case VK_RETURN:
break;
case VK_HOME:
break;
case VK_END:
break;
case VK_PRIOR:
break;
case VK_NEXT:
break;
case VK_LEFT:
break;
case VK_RIGHT:
break;
case VK_UP:
break;
case VK_DOWN:
break;
case VK_DELETE:
break;
case VK_BACK:
break;
}
}
else if (WM_CHAR == Msg.message)
{
switch (Msg.wParam)
{
case 0x08:
break;
case 0x09:
break;
case 0x0D:
break;
case 0x0A:
break;
case 0x1B:
fEditing = false;
break;
default:
break;
}
}
else if (WM_KILLFOCUS == Msg.message)
{
fEditing = false;
}
}
}
|
|
|
|
|
Hello,
Has anyone of you managed to compile the LIBPNG[^] (1.2.15) to a DLL with MS Visual C++ 8.0 (2005)?
I'd be very thankful if you could share your .vcproj file with me.
|
|
|
|
|
VS2005 can load a PNG, why do you need LIBPNG ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Thank you for your reply.
I have a specialized library that is practically independent from the Platform SDK, and uses LIBPNG. I would like to include this library in my VS 2005 solution without modifying its code too much.
|
|
|
|
|
Hi folks,
I’ve got a problem getting a pointer to the active document from an MDI. The code below works fine for an SDI, but when I run the binary of the MDI it throws an unhandeled exception error upon trying to access data from the document. When I’m in the editor, however, the intellisense works just fine, ie, when I type ‘pDoc->’ it retrieves the right info from the current document. My question is whether I have to do any additional thing to get the statement below to work for MDIs and what that statement might be.
CMtxDoc *pDoc = (CMtxDoc *)
((CFrameWnd*) AfxGetApp()->m_pMainWnd)->GetActiveDocument();
CString szHold;
pDoc->m_nDim = 2; //exception thrown here
Thx,
Ralf.
ralf.riedel@usm.edu
|
|
|
|
|
How many document types are there?
Steve
|
|
|
|
|
One type only
ralf.riedel@usm.edu
|
|
|
|
|
What is the value of "pDoc" when the crash occurs?
Steve
|
|
|
|
|
For MDI isn't the active document associated with a view in an MDI child?
I'm assuming m_pMainWnd points to a CMDIFrameWnd...
CMDIChildWnd *pActiveMDIChild = ((CFrameWnd*) AfxGetApp()->m_pMainWnd)->MDIGetActive();
CMDIChildWnd *pActiveMDIChild = ((CMDIFrameWnd*) AfxGetApp()->m_pMainWnd)->MDIGetActive();
if (pActiveMDIChild)
{
CMtxDoc *pDoc = (CMtxDoc *)pActiveMDIChild->GetActiveDocument();
...
}
Mark
-- modified at 19:25 Tuesday 16th January, 2007
|
|
|
|