|
Sander Rossel wrote: NaN will behave differently, feels very random. Sander, this is a brilliant idea you've come up with; I'll offer a quick sketch, and look forward to your comprehensive implementation:
namespace CatState
{
public static class CatStateExtensions
{
public static double CatDead(this double[] args)
{
if (args.Contains(double.NaN))
{
Random rnd = new Random((int) DateTime.Now.Ticks);
return (rnd.Next(0,2) == 0)
? double.PositiveInfinity
: double.NegativeInfinity;
}
return args.Min();
}
public static double CatAlive(this double[] args)
{
if (args.Contains(double.NaN))
{
Random rnd = new Random((int) DateTime.Now.Ticks);
return (rnd.Next(0,2) == 1)
? double.PositiveInfinity
: double.NegativeInfinity;
}
return args.Max();
}
}
} The half-dead cats fighting over half-dead fish-heads have been keeping me up nights, lately.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
BillWoodruff wrote: Random rnd = new Random((int) DateTime.Now.Ticks);
Reseeding RNG each time you make a call with nothing more than current time as a source of randomness, that's just a disaster waiting to happen. Output of your methods would be easily predictable. Nobody want their Schrödinger's cat predictable
|
|
|
|
|
Hi Mladen,
DateTime.Now.Ticks "is the number of 100-nanosecond intervals that have elapsed since 1/1/0001, 12:00am."
Yes: if you had a loop calling that function faster than 100 ns., you could get a duplicate seed, and what you suggest is better practice.
If "heavier-duty randomness" were required I would use the Crypto library.
In real-world code, I would create a single static instance of 'Random, and re-use it.
I wrote that code while I was half-dead, although that's a very poor excuse
cheers, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
BillWoodruff wrote: I wrote that code while I was half-dead, although that's a very poor excuse Not if you're actually Schrödinger's Bill!
|
|
|
|
|
The behaviour of comparing with Nan is defined.
The implementation of Min and Max is probably not. But a Min function is usually implemented as
x < y ? x : y
instead of
!(x >= y) ? x : y
Overall, comparing with NaN makes no sense as already noted by others. Just change the order of your elements (e.g. NaN as first element).
|
|
|
|
|
Jochen Arndt wrote: The implementation of Min and Max is probably not. But a Min function is usually implemented as
x < y ? x : y
instead of
!(x >= y) ? x : y Actually Min and Max don't treat NaN as they should to get predictable results, as pointed out by Mladen
|
|
|
|
|
I don't saw Mladen's reply when I wrote mine (late in the night) but it explains what happens here.
But that implementation produces predictible results:
If an element is NaN, it is the smallest number and returned.
How should it be treated else?
The only other option from my point of view would be throwing an execption (e.g. by using signaling NaNs instead of quite NaNs).
|
|
|
|
|
I can sort of see how that might happen, but I would prefer Min and Max ignored the NaN unless they had no choice (if it's the only thing in the list, there's no better choice).
|
|
|
|
|
|
Thanks, good find! At least that clears that up!
|
|
|
|
|
By the way, the email for this message had a YouTube link attached to it. I just found it in your sig
And I would once again argue that the existence of NaN is just plain wrong.
Well, at least you're right about the Lounge
|
|
|
|
|
Did you know that both 1 < double.NaN and 1 > double.NaN are false?!
|
|
|
|
|
I thought Julian Assange had called off his latest announcement of new WikiLeak content: [^].
Revelations like this, calling into question the entire structure of the way we view reality and the code tools we use to model it, is going to really shake things up.
The thought of the vast infinite hordes that NaN could mobilize and unleash on Primes and NotPrimes and SubPrimes ... frightening. I'm going to start building my bunker, now.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
Doom is upon us!
Or will be anytime soon now!
|
|
|
|
|
I know, which makes its behavior in Min and Max even more random
Although, as it turns out, it's not random at all. Min and Max just treat NaN as lower than anything else.
|
|
|
|
|
You need to check for NaN before passing to min/max and eventually root out them by code.
What happened to the simple rule of checking for unexpected/invalid values before using them? Does everybody now cross the streets without looking, eventually launching an exception if hit by a car?
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|
|
den2k88 wrote: What happened to the simple rule of checking for unexpected/invalid values before using them? Now you're talking a religion of which I am a true believer ! So many questions on C# QA could be answered by the posters, themselves, if they had learned to check input values before calling them, and how to use a debugger.
I am also in favor of disabling, or hiding, UI controls that are irrelevant to the current context, or which would create errors if clicked, or, which should only be used after specific action(s) by the user.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
BillWoodruff wrote: I am also in favor of disabling, or hiding, UI controls that are irrelevant to the current context, or which would create errors if clicked, or, which should only be used after specific action(s) by the user. Absolutely. Even when I start not doing so I end up fixing it because during the tests I elephant up myself.
The real problem is that noone programs anymore: now there are frameworks! Never release resources anymore, there is The Framework. Never think about what you have to do, The Framework has already a solution for you! If the solution is not right for your problem, modify the problem! Don't write your components: The Framework is better and there's no discussion on it!
The Framework weights several hundred megabytes, has its own version of DLL hell which is not called Dll hell, can cease backwards compatibility every moment and lose its support or be replaced by The Next Framework, which is better! And incompatible. And so on so forth...
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|
|
I'm writing the function that will be called by users that didn't check their input.
At least I should know how to handle their crap
|
|
|
|
|
Remember that not only the users can insert unacceptable values but that other parts of your code can produce them. Either you're 100% sure that to some point in your code the values are all ammissible or you check them, or you document that the values must be checked beforehand.
For example I made some extrafast buffer rotation procedures in Assembler (we needed them) and they crash if the number of columns is not a multiple of 64. Since checking each time the function is called would lower the extra speed it is clearly documented to make sure the buffers are allocated in 64 colums multiples. Otherwise I should check them.
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|
|
IIRC, IEEE Std 754-2008 does define a set of recommended functions, including min() and max() of a vector of numbers. C# still uses IEEE Std 754-1985, so they have their own, quirky, handing of min() and max().
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
... it was announced in 2013, and I don't think you can buy it, but...
Remember Ring[^]
If you have a problem remembering the anniversary or her birthday, this could save your life (or worse, your marriage).
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hey good idea! matter of interest you didn't forget herselfs birthday did you
|
|
|
|
|
No chance: January 1st.
Which makes it impossible to forget, but a PITA to find a good present for that close to Crimble...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I feel your pain. The D is 27 December and impossible if you don't opt for alcohol based gifts. Luckily I take after him.
veni bibi saltavi
|
|
|
|
|