|
Hello all and congratulate for new theme of site.
float f1,f2;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = f1 == (f2 * 3.0);
WHY always b is 0 (false). Really aren't f1 and f2 equal?
If it is, why?
THankS in AdVanCE. . .
|
|
|
|
|
Because when you deal with floating point values, equality cannot be expressed.
because one of the number cannot be accurately be represented as a binary number thus leading to rounding errors that should be taken into account by the developers.
see this : http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]
Watched code never compiles.
|
|
|
|
|
Maximilien wrote: Because when you deal with floating point values, equality cannot be expressed.
Comparisons work very well with floating point numbers, e.g. equality. Arithmetics, on the other hand, is tampering with accuracy.
|
|
|
|
|
Most numbers cannot be stored accurately in a float. Those numbers have to be rounded off to some near representation, 10/3 being one. Nothing to be upset about. It's just another day at the office for your computer.
Read more here[^].
|
|
|
|
|
i remember,when i was a student,
my c language teacher told me , if i compare two "float" or "double", i should check it between a very little number.
such as ,
const float c_tmp = 0.000001;
bool b;
float f1 = 10.0;
float f2 = f1 / 3;
b = (f1 >= (f2 * 3.0) - c_tmp) && (f1 <= (f2 * 3.0) + c_tmp);
|
|
|
|
|
or you could write
double difference = f1 - ( f2 * 3.0 );
bool b = fabs( difference ) < epsilon;
I think that is a little more self-explanatory plus it is faster since the calculations and comparisons are only performed once.
|
|
|
|
|
10 divided by 3 is 3.333 recurring in decimal and in binary it's something like: 11.01010101 (can't be bothered working it out any further). Say you have a computer which stores floats as 6 bits for the non-exponent chunk of the number. It'd truncate the value of 10/3 to 11.0101. When you multiply it by three you get: 1001.1111 which is truncated to 1001.11 which is 9.75 in decimal. While real double precision numbers use oodles more bits to represent the non-exponential part you get the same problem.
Note that you've got a similar problem with order of evaluation: (A * B ) / C for a computer using a finite number of bits is not necessarily equal to ( A / C ) * B. Fortunately most real world uses of computers don't have these problems - it's really only a concern to people doing simulation or some science. The rest of us can use integers most of the time and get away with it.
Cheers,
Ash
|
|
|
|
|
In the code, you do this: float f2 = f1 / 3;
I believe you're looking for this: fload f2 = f1 / 3.0;
I'm not sure it's going to set b = true, however, it's more correct nontheless.
|
|
|
|
|
Hello people!
I'm experimenting with DLL injection and API hooking, there's no real goal to it, just playing around. So, injecting a DLL into a running process and hooking methods works fine, i have done that a few times already in the past so i have some experience.
Now, what i tried this time is to create a process with CreateProcess[^], specifying the flag CREATE_SUSPENDED to create the process' main thread suspended, then, i used the CreateRemoteThread method to inject a DLL, do some API hooking, and then called ResumeThread on the process' main thread. When i do this i hear a *clonk* soundeffect and the runned process does not start/exists prematurely. So i wonder, what could cause this? If i run the process "normally" and inject the DLL (with the same remote thread method) it works as expected.
Can anyone shed some light on this? Thanks for the answers.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
I use ActiveX container to host a Word document in my app. I want to hide Word toolbar and
menu bar.
I tried to use the following code
// app is an instance of _Application
CCommandBars cbs = app.GetCommandBars();
for (int i=1; i<=cbs.get_Count(); i++)
{
CCommandBar bar = cbs.get_Item(COleVariant((short)i));
bar.put_Enabled(false);
//bar.put_Visible(false);
}
I tried put_Enabled, it won't hide toolbar/menu bar.
I also tried put_Visible but it caused the Word document being closed.
I am wondering if some one can point me direction here.
|
|
|
|
|
I want to build a SSL server application in VC++, unmanaged code, using OpenSSL.
I got many sample application of SSL Client. but the server sample application is not so frequent, the application which I got was not working . The problem I am facing is that I am not sure about the method of developing a server application and the usage of digital certificate. Can anybody help me how to do it or send me some useful links.
Any help will be appreciated
Thanks in advance
Regards
Rajmohan
|
|
|
|
|
Beg, borrow or steal a copy of "Network Security with OpenSSL" by Viega, Messier and Chandra. It doesn't describe the SSL protocol that well but it has a recipe for a client server app including where certificate checking fits in.
Cheers,
Ash
|
|
|
|
|
Thank you so much for the reply.
Regards
Rajmohan
|
|
|
|
|
A const string reference is being passed just as a string to another proc.The code crashes after the call a_proc(). Please give your inputs
Imp::Reset(const string& res_str, const bool& b)
{
a_proc(res_str);
a_get();
}
Imp::a_proc(string res_str)
{
//res_str is just validated
}
|
|
|
|
|
Nothing wrong with passing the string a const reference, it's not the cause of the crash. Use your debugger to see where the crash occurs precisely and for what reason.
|
|
|
|
|
The Issue now is passing the variable that was retreived as a const string& is passed to another proc as just a string(passing by value). Any comments on this.
sorry, I don't have a debugger.
|
|
|
|
|
As I said, there's nothing wrong with passing a string by const reference and it won't make any different. You are focusing on the wrong problem and should try to find the real cause of the problem.
apphia_j_79 wrote: sorry, I don't have a debugger.
With which IDE are you coding ? Developping without a debugger is like cooking without knives
|
|
|
|
|
Probably you should post the actual code to get better help.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
And while you're thinking of posting your code consider why you're passing the string by value to a_proc and using a const reference to bool in Reset.
The first one I can see a point for if you were going to copy the string anyway to mangle it but the const bool &b thing doesn't make a lot of sense.
Oh, hang on, you're not casting it to something else in the function to modify it are you?
Ash
|
|
|
|
|
You should check the string before sending it to Reset, or the object owning the string. Taking its reference might be ok even if the instance no longer exists, but once you dereference it, you might get an access violation.
Typically, problems like this occurs if your string is a member variable in an instance you have deleted (or is null).
class A
{
string s;
public:
void method() { Reset(s); }
};
A *a = nullptr;
a->method();
The above code will work when Reset() takes a reference to the string, but not if it accesses its value, which a call-by-value does.
|
|
|
|
|
Hi!
I got the following while compiling my program.
d:\test\test\nrp2dpictureflow.cpp(90) : error C2782: 'const T &irr::core::min_(const T &,const T &,const T &)' : template parameter 'T' is ambiguous
1> e:\irrlicht-1.7.1\include\irrmath.h(123) : see declaration of 'irr::core::min_'
1> could be 'irr::u32'
1> or 'int'
1>d:\test\test\nrp2dpictureflow.cpp(90) : error C2780: 'const T &irr::core::min_(const T &,const T &)' : expects 2 arguments - 3 provided
1> e:\irrlicht-1.7.1\include\irrmath.h(116) : see declaration of 'irr::core::min_'
Th function min_ is defined as follows:
template<class T>
inline const T& min_(const T& a, const T& b)
{
return a < b ? a : b;
}
template<class T>
inline const T& min_(const T& a, const T& b, const T& c)
{
return a < b ? min_(a, c) : min_(b, c);
}
Why the compiler expects the third value? What value do I have to pass as third argument to min_ ?
|
|
|
|
|
And what is your code actually ?
|
|
|
|
|
Check the way you're calling the function. The parameter you pass must have all the same type (or decade implicitly all into a same common type). If -for example- you pass an int and a float the compiler cannot decide which has to be converted into the other.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
is there any API which i can import a registry file in the registry editor? moreover in my registry file is a key which need full control permission for everyone in HKEY_LOCAL_MACHINE, how can i get this through source?!
thanks
|
|
|
|
|