|
I suspect everyone has run unti this, but I just ran into it.
double d1 = 1;
When I look at d1 in the debugger, it will show 1.0000001342, or something similar. Now, that is close to 1, but it's not the same as 1.
d1 - 1 is then equal to .0000001342.
And .0000001342 isn't zero.
Suggestions as to how to handle this? How do I get 1.0000000000 rather than 1.0000001342?
I know I can use an int for 1, but the same thing occurs with:
double d2 = 2.354
debugger shows d2 as 2.3540000154.
Thanks
|
|
|
|
|
I can't reproduce this on VC++ 2003 .NET SP1
|
|
|
|
|
It is inherant in how floating point works that many numbers cannot be precisely represented with a finite number of bits. Just as in base 10, a number such as 1/3 needs an infinite number of digits to be written out (0.33333.....)
Take, for example, the number 1/10. Suppose you write double d1 = 0.1 - internally this decimal is converted into binary which (simplfying somewhat, floating point numbers are actually stored in the binary version of scientific notation) is 0.0001100110011001100... where the bits to the right of the point at 1/2, 1/4, 1/8, 1/16, 1/32, etc. Since any actual data type has a finite number of bits (in the case of double precision floating point, 53 bits are available from the first "1") we can only store an apporximation of many values.
When using floating point data, you should never test numbers for equality (unless you're certain that the numbers need to be the same sequence of bits) you should instead check if the difference between two numbers is sufficently small.
|
|
|
|
|
AJR_UK wrote: It is inherant in how floating point works that many numbers cannot be precisely represented with a finite number of bits
So you're saying 1 can't be precisely represented in a double?
|
|
|
|
|
Bah, smart-alec. <pedantic>I only said "many numbers", I didn't say anything about which numbers, other than the example I gave.
1 (or any other integer until you get big enough that precision becomes an issue) can, of course, be precisely represented in a double. I dunno why Oliver was having problems when using the value 1, but I was addressing hte general case. That's my story, and I'm sticking to it
|
|
|
|
|
AJR_UK wrote: Bah, smart-alec
Sorry
Actually your post IMO was excellent and between you and Oliver you had me a bit worried
. I actually debugged a few examples and made sure!
Cheers!
|
|
|
|
|
I can't reproduce that, using Visual Studio 6.
Please give us info about your env.
|
|
|
|
|
I have following piece of code, which works in VC7 but not in VC6. Don't know what's the problem.
A.h
----
class A
{
private:
template[int N] //using sqare brackets as somehow while posting it ignores '<'
class B {
public:
B() {}
class C{
public:
C() {}
C* GetSomething() const;
}; //end class C
}; //end class B
};//end class A
A.cpp
------
template[int N]
typename A::B[N]::C*
A::B[N]::C::GetSomething() {}
RP
|
|
|
|
|
Possibly because VC6 is rubbish ? VC7 was a huge jump in standards conformance. It especially sucked at template support.
To get the < to show, check the 'ignore HTML tags' checkbox.
|
|
|
|
|
hello......
i need help for how to read/write file in C++
i use fstream but i wont to open file one time for all functions(file is glopal)
and i wont the file be open in the constructor of my class.
where can i declerd this lines
ftream myfile;
myfile.open(.....,.......);
is it in main or in class?
i have decler in main function but ather function be ERROR.
thanks for all.
|
|
|
|
|
This should be outside of any function or class:
fstream myfile;
This could be at the beginning of main():
myfile.open(.....,.......);
This could be at the end of main():
myfile.close();
Use this in other modules so they know where to link to myfile:
extern fstream myfile;
|
|
|
|
|
You can declare it where-ever you like, but it makes more sense to not hold a file open while your app is running.
Posting some code and error messages would help.
|
|
|
|
|
When I'm using icons in my application, I get a funy dark border around them that results in them having jagged edges, even though their smooth when their placed onto the dialog or elsewhere in the software...any thoughts about how to stop that?
Thanks,
BP
|
|
|
|
|
BlitzPackage wrote: ...I get a funy dark border around them that results in them having jagged edges, even though their smooth when their placed onto the dialog or elsewhere in the software...
Where are they when they have the dark border and jagged edge if they are smooth when you
actually use them?
|
|
|
|
|
When I compile and run the software they show with the dark border and jagged edges. What's causing that?
|
|
|
|
|
BlitzPackage wrote: When I compile and run the software they show with the dark border and jagged edges. What's causing that?
Icons created in VS or at runtime? Bit depth? How are they being used? Are you drawing them?
|
|
|
|
|
I bought them from a site called "Icon Galore". They looked fine and they provided several formats (.ico, .gif, .png), sizes, and bits (8 and 32 bits), with true color, 256 colors, and black and white. The gifs look fine, but of course, I can't use that in VS. I just import them as an icon, and place them on the dialog via the picture control (selecting icon as the type). When I convert them to a bitmap with Corel Draw, the display real smooth except for the fact that it gives them a white background.
What should I do?
Any help or answers you can provide would really be appreciated. Thanks for your time.
Sincerely,
BP
|
|
|
|
|
BlitzPackage wrote: I just import them as an icon, and place them on the dialog via the picture control (selecting icon as the type).
What happens if you add the SS_REALSIZEIMAGE style to the picture control? Just thinking
maybe they get messed up when/if being resized.
|
|
|
|
|
Hmmmm...how do I add that style?
|
|
|
|
|
Highlight the picture control, F4 for properties, set "Real Size Image" property to true;
Note this will clip the icon if your icon is bigger than the picture control but at least
you can see if it draws properly.
|
|
|
|
|
Watch out for using 24 bit icons using a mask/transparent pixel value. That'll never look well. Try to use 32 bit icons with alpha channel present - it produces awesome results.
Also, if you have 32 bit icons - DO NOT open them in Visual Studio. It'll strip out the alpha channel information, making the icons look like crap.
--
Made From Meat By-Products
|
|
|
|
|
How do I ensure that alpha channels are present? Also, how do I use them in my project, but not open them in Visual Studio (which I am using for my project)? By the way, when I convert them into a bitmap (using Corel Draw 11), they are smooth but with a white background. Is there a way I could get rid of that white background?
Thanks for any help you can provide.
Sincerely,
BP
|
|
|
|
|
Hello,
Is it possible to emulate keyboard event (using keybd_event) on the desktop, that is different from active (input) desktop? I.e. I want to create a thread, that calls SetThreadDesktop() and then emulates some keyboard activity on the desktop, whos handle was passed to SetThreadDesktop().
Currently, I have no result until I pass handle of input desktop to SetThreadDesktop().
|
|
|
|
|
Hi,
I am trying to write a stack trace function for an application.
If an exception occurs, I need the stack trace to be printed.
I am using stackwalk64 API to read the stack. But some times
the exception record retreived is improper resulting in inadequate
or junk reports. So I plan to access the stack pointer directly using
Inline Assembly. But the SetUnhandledException
pops the stackframes soon after the crash(before the filter function is called).
So Is there any API which simply notifies the occurence of the exception
without popping the stack soon after the exception occurs.
Thanks in Advance.
-Mathespa
|
|
|
|
|
You're best off just letting the application crash in most cases. Dr. Watson will generate a stack dump and if need be you can configure it to produce a crash dump file which you can load up with WinDBG (and other tools) to get stack traces, examine memory and more. In short you get more functionality for free by not doing anything.
Steve
|
|
|
|