|
This is really too pessimistic
No, realistic. Far too many people use floating point numbers where they are not necessary, or actually wrong.
|
|
|
|
|
That is true, but "you can never rely on two numbers being the same" is not. There are conditions that can be satisfied that will make the numbers the same.
|
|
|
|
|
Yes, but you understand floating point representation. Too many developers do not appear to share that understanding.
|
|
|
|
|
So with your Argumentation one Need also to say: Don't use int_64 ... which of course makes no sense.
A double value has a discret number of numbers it can represent, same as an int_64 has
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
No, that is not what I am saying at all. And it appears, that like so many people on these forums, you also do not understand how floating point numbers work.
|
|
|
|
|
Try this
#define NUMBER 133333333
int main()
{
int nValue = NUMBER;
float fValue = NUMBER;
printf("%d\n", nValue);
printf("%f\n", fValue);
|
|
|
|
|
Thank you very much for this. I'm completely aware of such effects and also on the normalizing stuff.
Thank you again
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Quote: Is my idea too dirty?
I have just the same feeling about.
|
|
|
|
|
I'm a bit late to the party.
The key point of a map is that no two elements in a map container can have equivalent keys. If the number of significant digits entered is less than the precision of double values, this will not happen (besides entering the same value again which applies to int s as well).
But you might get problems when trying to access elements by key when the argument is from other sources (affected by rounding).
That would be my bad feeling:
While there is no problem with the map itself, key based operations ([]operator , erase , swap , find ) might behave not as expected (no match). Even when not using such operations now but only iterator based ones, they may be used later when updating the code. So you should at least place a comment about this.
Another option is creating your own std::map based class that disables the affected operations or replaces them with a version that treats close numbers as identical (defining an epsilon).
|
|
|
|
|
Thank you very much for your comments and your time.
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
0x01AA wrote: std :: map<double, MyType, std :: less<double> >
Hi,
I was avoiding this discussion but I wanted to just add one more comment. The FPU has a state that has an affect on the precision. In other words... when the FPU is in various states you get a different rounding value. You can control this state with _controlfp_s
Printer DLL and other libraries outside of your control could potentially change the thread FPU state causing your std::map to contain multiple floating point numbers from the same calculations. Unfortunately on Windows user-applications cannot always control third-party DLL from loading into your process. Those DLL could modify and then fail to reset the floating point state.
The answer is "Yes" what you are proposing would work 99% of the time or in "laboratory" conditions where you control both the hardware and software. But if you tried to ship out a commercial product using the above technique... you will almost certainly have a small percentage of the conditions I described above.
Best Wishes,
-David Delaune
|
|
|
|
|
Wow, great that you share this knowledge.
Thank you very much for this.
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Hi,
I need basics to know about COM/DCOM. Any good article, please point me the link
Thanks,
|
|
|
|
|
|
Hi,
What is the recommended way to listen for a key press globally? At first, adding an infinite loop came to mind (for(;;), or while(true)), but these are bad choices, as they eat a lot of CPU, and adding sleep will cause to lose some of the key press events.
I want to make it to check any key (or key combination), to see what is pressed on Windows OS.
|
|
|
|
|
It depends if "globally" applies to the system (user session) or an application. For an application it depends on the type (console or GUI). With GUI applications it depends also if you want to catch all / multiple or only a single / few key combinations.
The most "global" method is hooking keyboard events with SetWindowsHookEx function (Windows)[^].
For application level handling see About Keyboard Input | Microsoft Docs[^] and follow the links.
To handle only a few specific keyboard events for GUI applications use Accelerators (Hotkeys) processed by the main window of the application.
|
|
|
|
|
This function will be called from Java, which app will be minimized, or maybe running in the background. The app won't be focused, so yes, I need it for the entire user session, even if another application is focused, I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A", those combinations will be changeable.
|
|
|
|
|
You might have a look at the AutoIt Scripting Language. I guess you can use that to do what you want.
In any case you should not use a key combination that is used by Windows itself or common applications. See Keyboard shortcuts in Windows for a list of combinations that should not be redirected.
|
|
|
|
|
Valentinor wrote: I want it to still register the key press and do something in case a key I randomly choose is pressed. I will make some profiles: profile 1 has key "A", profile 2 has key "CTRL + A",
Sounds like you are looking for the RegisterHotKey function[^].
Best Wishes,
-David Delaune
|
|
|
|
|
For what I have now, I'll use only one key, and I used "SetWindowsHookEx". Is it possible to use that for a combination of keys? I don't need it in this application, but in future I will need it. All the examples I saw on google were with only 1 key, or using the mouse. If you can use it for key combination, can it be used to combine 4-5 keys? Yes, it is hard to press that many at a time, but you can map an extra key from the keyboard to press all of them at the same time.
Thanks for you help!
|
|
|
|
|
With key combinations I meant a combination of a single key while also Shift, Ctrl, and or Alt are down.
A combination of multiple keystrokes makes no sense because that can't be "eaten" (the initial strokes has to be passed and will be processed by the window having the focus).
It is still unclear what you finally want to do. If is not a personal project, be very careful what you are doing. Such global "features" might interfere with the active application which is a "don't do it".
|
|
|
|
|
So, let me reformulate my last statement.
What is the alternative of SetWindowsHookEx, for a combination of keys, by checking if Ctrl and/or Alt and/or Shift is down + any other key (different from Windows combinations)?
For a single button (keyboard or mouse) it is great, but from what I've seen, it can't be used to check at the same time if any Ctrl, Alt, Shift is down.
|
|
|
|
|
The status of the Alt key is passed to the hook callback. The status of the Shift and Ctrl keys (and any other key) can be determined with the GetKeyState function (Windows)[^].
|
|
|
|
|
|
Hello,
I have registered USB serial devices for notification and get a device arrival & removal notice now in OnDeviceChange. However, always when I try to print out the dbcc_name, its length is one wide character (as defined in the structure documentation) and nothing more prints out. I have browsed countless examples where the code proceeds to extract for instance a VID/PID from the string. What am I doing wrong? The examples also give me one character. I am using windows 10 & VS2017.
I have even reserved a larger buffer for notification registration to fit in the full dbcc_name string. Nothing helps, just get that one character. I have also tried the WideCharToMultibyte and then instead of a Chinese character I get a single question mark. Please advise.
|
|
|
|