Campbell Ritchie wrote:He did say that factorials were only defined for positive numbers.
Yes he did, I thought from programmers perspective. Indeed, the topic of video was just about recursion so validations shouldn't be expected.Campbell Ritchie wrote:He did say that factorials were only defined for positive numbers.
I googled about It, I found some say natural numbers begin with 0 whereas others say begin with 1. Here Wiki: Natural numberSaying factorials were only defined for positive integers rather than natural numbers which include 0
I shall let you explain the output for 64! and 66! and −64!
You need to be very careful about such websites. You need to choose the one that matches your present requirements, so those that say 0 is a natural number are correct. But when you want 0 not to be a natural number, you look at the other websitesGanesh Patekar wrote:. . . some say natural numbers begin with 0 whereas others say begin with 1. . . .
Where did you find that about not overflowing? Surely what you describe is overflow? Remember that every time you multiply by an even number you add at least one 0 bit to the right end of the number and when you get to × 66, you have added 64 0 bits, so you get 0. 21! is the last factorial which will fit into 64 bits; all values above 21! suffer overflow errors.. . . When result of multiplication of unsigned numbers goes beyond the max range i.e. 18446744073709551615 they can't overflow . . .I shall let you explain the output for 64! and 66! and −64!
But unsigned doesn't permit negative numbersWhen ___* -64 happens
It will keep calling factorial(unsigned long int i) like factorial( -64 - 1 ) * -64 then factorial( -65 - 1 ) * -65 then factorial( -66 - 1 ) * -65 then.... at last gives Segmentation fault. . . .
That's what I didCampbell Ritchie wrote:But when you want 0 not to be a natural number, you look at the other websites
Here Yes It overflows, so many sites says that but why remember this one only dunno, I think I had sleepy eyesWhere did you find that about not overflowing? Surely what you describe is overflow?
But unsigned doesn't permit negative numbers
There are all sorts of odd things in that SO link; I still cannot see that calculating the number modulo 2⁶⁴ is any different from overflow. I am also surprised that he suggests that signed integer overflow is unpredictable; I always thought overflow is always predictable. Of course there are lots of things which are undefined in C and strictly defined in Java®.Ganesh Patekar wrote:. . . Here Yes It overflows, so many sites says that but why remember this one only dunno, I think I had sleepy eyes
. . .
‐4 = 18446744073709551612
‐10 = 18446744073709551606 . . .
The CERT® C Coding Standard, Second Edition by Robert C. Seacord, Ch 4. Page No 112 wrote:The C Standard, 6.2.5 paragraph 9, States: "A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. This behavior is more informally called unsigned integer wrapping.
Undefined behavior, Wiki wrote:In computer programming, undefined behavior (UB) is the result of executing computer code that does not have a prescribed behavior by the language specification the code adheres to, for the current state of the program (e.g. memory). This happens when the translator of the source code makes certain assumptions, but these assumptions are not satisfied during execution.
The behavior of some programming languages - most famously C and C++ - is undefined in some cases.
Undefined behavior is often unpredictable and a frequent cause of software bugs. In the C community, undefined behavior may be humorously referred to as "nasal demons", after a comp.std.c post that explained undefined behavior as allowing the compiler to do anything it chooses, even "to make demons fly out of your nose"
Beneath Benefits from undefined behavior It says:
The value of x cannot be negative and, given that signed integer overflow is undefined behavior in C.
..... ....
..... ....
After second example:
Another benefit from allowing signed integer overflow to be undefined is that it makes it possible to store and manipulate a variable's value in a processor register that is larger than the size of the variable in the source code.
I remember, the first language I learned was Pascal in 2007 then C, VB, C++, DS, SQL, Java etc etc. We used to learn programs by dry run on papers and C's pointers and string programs were quite interesting. After long long time I did a C program. Thank you for being the reason behind ItCampbell Ritchie wrote:You have put no end of effort into sorting out this problem
Stephan van Hulst wrote:Understanding recursion lies in believing that the recursive step "just works".
Don't get me started about those stupid light bulbs. |