int nan

2009-06-26 Thread bearophile
The following comes partially from a friend of mine. If you are busy you can skip this post of musings. >From the docs: http://www.digitalmars.com/d/1.0/faq.html#nan >Because of the way CPUs are designed, there is no NaN value for integers, so D >uses 0 instead. It doesn't have the advantages of

Re: int nan

2009-06-26 Thread dsimcha
== Quote from bearophile (bearophileh...@lycos.com)'s article > The following comes partially from a friend of mine. If you are busy you can skip this post of musings. > From the docs: > http://www.digitalmars.com/d/1.0/faq.html#nan > >Because of the way CPUs are designed, there is no NaN value for

Re: int nan

2009-06-26 Thread Nick Sabalausky
"bearophile" wrote in message news:h237c9$or...@digitalmars.com... > The following comes partially from a friend of mine. If you are busy you > can skip this post of musings. > > From the docs: > http://www.digitalmars.com/d/1.0/faq.html#nan >>Because of the way CPUs are designed, there is no Na

Re: int nan

2009-06-27 Thread bearophile
Nick Sabalausky: > Ie, Default initing to NaN is certainly better than > default-initing to a commonly-used value, but it still isn't the right > long-term solution. Having a nan has other purposes beside initialization values. You can represent missing values, like C# nullable ints (that are b

Re: int nan

2009-06-27 Thread BCS
Hello Nick, Interesting idea, but IMO using NaN as a default initializer is just a crutch for not having a real system of compile-time detecting/preventing of uninitialized variables from being read (C#'s system for this works very well in my experience). I think you can prove that it is impos

Re: int nan

2009-06-27 Thread Michiel Helvensteijn
BCS wrote: >> Interesting idea, but IMO using NaN as a default initializer is just a >> crutch for not having a real system of compile-time >> detecting/preventing of uninitialized variables from being read (C#'s >> system for this works very well in my experience). > > I think you can prove that

Re: int nan

2009-06-27 Thread superdan
Michiel Helvensteijn Wrote: > BCS wrote: > > >> Interesting idea, but IMO using NaN as a default initializer is just a > >> crutch for not having a real system of compile-time > >> detecting/preventing of uninitialized variables from being read (C#'s > >> system for this works very well in my exp

Re: int nan

2009-06-27 Thread Michiel Helvensteijn
superdan wrote: >> Complete static analysis of the flow of program control is the holy grail >> of compiler construction. It would allow automatic proof of many program >> properties (such as initialization). It may not be impossible, but it is >> extremely complicated. > > extremely complicated?

Re: int nan

2009-06-27 Thread Denis Koroskin
On Sat, 27 Jun 2009 17:50:11 +0400, BCS wrote: Hello Nick, Interesting idea, but IMO using NaN as a default initializer is just a crutch for not having a real system of compile-time detecting/preventing of uninitialized variables from being read (C#'s system for this works very well in my exp

Re: int nan

2009-06-27 Thread Michiel Helvensteijn
Denis Koroskin wrote: >> int i; >> >> for(int j = foo(); j > 0; j--) i = bar(j); // what if foo() returns >> -5? > > This code doesn't compile in C# and fails with the following error at > first attempt to use 'i': > > error CS0165: Use of unassigned local variable 'i' Ah, so C# is overly con

Re: int nan

2009-06-27 Thread Walter Bright
Michiel Helvensteijn wrote: * Add 'uninitialized' to the set of possible states of each type. Every time a variable is read, assert that it is initialized first. Use the static analysis techniques that *are* available (a set that will continue to grow) to eliminate these tests (and the extended s

Re: int nan

2009-06-27 Thread Nick Sabalausky
"bearophile" wrote in message news:h250ve$1dv...@digitalmars.com... > Nick Sabalausky: >> Ie, Default initing to NaN is certainly better than >> default-initing to a commonly-used value, but it still isn't the right >> long-term solution. > > Having a nan has other purposes beside initialization

Re: int nan

2009-06-27 Thread Nick Sabalausky
"Michiel Helvensteijn" wrote in message news:h25fbk$28m...@digitalmars.com... > Denis Koroskin wrote: > >>> int i; >>> >>> for(int j = foo(); j > 0; j--) i = bar(j); // what if foo() returns >>> -5? >> >> This code doesn't compile in C# and fails with the following error at >> first attempt to

Re: int nan

2009-06-27 Thread Nick Sabalausky
"Nick Sabalausky" wrote in message news:h2623m$73...@digitalmars.com... > "Michiel Helvensteijn" wrote in message > news:h25fbk$28m...@digitalmars.com... >> Denis Koroskin wrote: >> int i; for(int j = foo(); j > 0; j--) i = bar(j); // what if foo() returns -5? >>> >>> This

Re: int nan

2009-06-27 Thread BCS
Hello Denis, On Sat, 27 Jun 2009 17:50:11 +0400, BCS wrote: Hello Nick, Interesting idea, but IMO using NaN as a default initializer is just a crutch for not having a real system of compile-time detecting/preventing of uninitialized variables from being read (C#'s system for this works very

Re: int nan

2009-06-27 Thread BCS
Hello Nick, "Michiel Helvensteijn" wrote in message news:h25fbk$28m...@digitalmars.com... Ah, so C# is overly conservative. That's another option, of course. It has the advantage of always knowing at compile time that you're not reading an uninitialized value. The disadvantage is that C# wil

Re: int nan

2009-06-27 Thread grauzone
Having a nan has other purposes beside initialization values. You can represent missing values, like C# nullable ints (that are bigger in size, 8 bytes, I think). You're saying C# nullable ints require more memory than native ints, but just how would you represent int.nan with 32 bits? The

Re: int nan

2009-06-28 Thread bearophile
grauzone: > You're saying C# nullable ints require more memory than native ints, but > just how would you represent int.nan with 32 bits? Have you read my posts? I have said to use the value that currently is int.min as null, and I've explained why. I'll keep dreaming some more years, bye, be

Re: int nan

2009-06-28 Thread grauzone
Have you read my posts? I have said to use the value that currently is int.min as null, and I've explained why. That wasn't very explicit. Anyway, we need int.min for, you know, doing useful stuff. We can't just define a quite random number to be a special value. Checking math operations for

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Nick Sabalausky wrote: >> Yes, this approach is what I was getting at. In fact, I would (and >> already have in the past) argue that this is *better* than the "holy >> grail" approach, because because it's based on very simple and easy to >> remember rules. Conversely, the "holy grail" approach le

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Simen Kjaeraas wrote: >> But the beauty of the holy grail is that it's neither. > > While the ugliness of it is that it's both. Care to elaborate? -- Michiel Helvensteijn

Re: int nan

2009-06-28 Thread Simen Kjaeraas
Michiel Helvensteijn wrote: But the beauty of the holy grail is that it's neither. While the ugliness of it is that it's both. -- Simen

Re: int nan

2009-06-28 Thread Ary Borenszweig
Michiel Helvensteijn escribió: Nick Sabalausky wrote: Yes, this approach is what I was getting at. In fact, I would (and already have in the past) argue that this is *better* than the "holy grail" approach, because because it's based on very simple and easy to remember rules. Conversely, the "h

Re: int nan

2009-06-28 Thread Simen Kjaeraas
Michiel Helvensteijn wrote: Simen Kjaeraas wrote: But the beauty of the holy grail is that it's neither. While the ugliness of it is that it's both. Care to elaborate? As has already been mentioned, one of the biggest problems with the holy grail is that it leads to capricious states of

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Ary Borenszweig wrote: >> int i; >> assert(foo() > 3); >> for(int j = foo(); j > 3; j--) i = j; >> auto k = i; // Compiles at the moment... >> >> Would C# swallow that? > > Of course not: > > int foo() { >return rand() % 10; > } My mistake. For some reason I was assuming 'foo' was pure.

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Simen Kjaeraas wrote: But the beauty of the holy grail is that it's neither. >>> >>> While the ugliness of it is that it's both. >> >> Care to elaborate? > > As has already been mentioned, one of the biggest problems with the holy > grail is that it leads to capricious states of "possibly co

Re: int nan

2009-06-28 Thread Nick Sabalausky
"Michiel Helvensteijn" wrote in message news:h2810s$hl...@digitalmars.com... > Nick Sabalausky wrote: > >>> Yes, this approach is what I was getting at. In fact, I would (and >>> already have in the past) argue that this is *better* than the "holy >>> grail" approach, because because it's based o

Re: int nan

2009-06-28 Thread bearophile
grauzone: > That wasn't very explicit. Anyway, we need int.min for, you know, doing > useful stuff. Like for what? Have you used a Lisp? Their tagged integers show that a smaller range is fine. And I'm just talking about 1 value in 4 billions, I don't think you will miss it much. And it's a val

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Nick Sabalausky wrote: > The fix would be one of the following, depending on what the code is > actually doing: > > --- > // Instead of knee-jerking i to 0, we default init it to > // whatever safe value we want it to be if the loop > // doesn't set it. This, of course, may or may not

Re: int nan

2009-06-28 Thread Nick Sabalausky
"Nick Sabalausky" wrote in message news:h28gqc$1du...@digitalmars.com... > "Michiel Helvensteijn" wrote in message > news:h2810s$hl...@digitalmars.com... >> >>> Additionally, in the C# approach (and this is speaking from personal >>> experience), anytime you do come across a provably-correct ca

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Nick Sabalausky wrote: > Ie, I can agree that the compiler should be able to take advantage of a > function's contract when determining whether or not to throw a "may not > get inited" error, but I strongly disagree that the contract used should > be implicity defined by the actual behavior of the

Re: int nan

2009-06-28 Thread BCS
Hello Nick, Also, keep in mind that while, under this mechanism, it is certainly possible for a coder to cause bugs by always knee-jerking the value to zero whenever the compiler complains, that's also a possibility under the "holy grail" approach. How about letting the user signal that they

Re: int nan

2009-06-28 Thread ponce
I'm sorry but I think it would be an ugly feature. What would be the NaN of uint ? What if you actually need 2^32 different values (such as in a linear congruential random number generator) ? Besides, there would be no cheap way to ensure NaN propagation (no hardware support). Cheers.

Re: int nan

2009-06-28 Thread Nick Sabalausky
"Michiel Helvensteijn" wrote in message news:h28i61$1hl...@digitalmars.com... > Nick Sabalausky wrote: > >> The fix would be one of the following, depending on what the code is >> actually doing: >> >> --- >> // Instead of knee-jerking i to 0, we default init it to >> // whatever safe

Re: int nan

2009-06-28 Thread bearophile
ponce: > What would be the NaN of uint ? Having a NaN in just signed integral values (of 1, 2, 4, 8, 16 bytes) looks enough to me, see below. >What if you actually need 2^32 different values (such as in a linear >congruential random number generator) ?< I agree that there are many situations

Re: int nan

2009-06-28 Thread Jarrett Billingsley
On Sun, Jun 28, 2009 at 6:02 PM, bearophile wrote: >>Besides, there would be no cheap way to ensure NaN propagation (no hardware >>support).< > > I was talking about having hardware support, of course. Let me know when x86 gets that.

Re: int nan

2009-06-28 Thread Michiel Helvensteijn
Nick Sabalausky wrote: > Init, assert, spell-check land, etc. We're agreed. >>> Also, keep in mind that while, under this mechanism, it is certainly >>> possible for a coder to cause bugs by always knee-jerking the value to >>> zero whenever the compiler complains, that's also a possibility unde

Re: int nan

2009-06-28 Thread Nick Sabalausky
"Jarrett Billingsley" wrote in message news:mailman.315.1246226874.13405.digitalmar...@puremagic.com... > On Sun, Jun 28, 2009 at 6:02 PM, bearophile > wrote: > >>>Besides, there would be no cheap way to ensure NaN propagation (no >>>hardware support).< >> >> I was talking about having hardware

Re: int nan

2009-06-29 Thread Frits van Bommel
bearophile wrote: grauzone: That wasn't very explicit. Anyway, we need int.min for, you know, doing useful stuff. Like for what? Have you used a Lisp? Their tagged integers show that a smaller range is fine. And I'm just talking about 1 value in 4 billions, I don't think you will miss it muc

Re: int nan

2009-06-29 Thread bearophile
Frits van Bommel: > It's fine for Lisp because any Lisp I've ever seen auto-upgrades out-of-range > integers to (heap-allocated) bigints. I think it can be fine even if you have just fixnums with that single value missing from signed integrals. > I'd like to point out you don't need a new buil