Are BrainF*ck tape spots overflowing? [duplicate] - integer-overflow

What happens in Brainfuck if I try to "-" in a cell that contains a 0?
>-
Also, what happens if I try to start a loop while pointing to a 0 cell?
>[]
Edit:
I forgot to mention it, but I'm trying to make an interpreter.
Here's a piece of code I was given to use as an example:
;>;<[->++<][->++<]
In the second loop, the cell pointed at is "0", so my program starts an infinite loop.

Brainfuck is a very implementation dependent language.
Usually, Brainfuck cells hold values ranging from 0-255, so 8 bit unsigned integer values.
If you try to decrement a cell with a value of zero, as usually happens with computers you will perform an "underflow", meaning the value will go from 0 to 255. Similarly, if you try to increment a cell with a value of 255 - you will get 0.
Looping in brainfuck can be though of with this piece of pseudo code:
[SOME_CODE]
is like
while(*ptr!=0){SOME_CODE}
So long as you are not pointing at a cell with a value of 0 in the beginning of the loop, you run the code within the brackets, and repeat.
I suggest you take a look at https://fatiherikli.github.io/brainfuck-visualizer/

Most implementations of brainfuck will roll over from 0 to 255, and 255 to 0. Many programs will not work at all if they can't roll over.
In your program example, the ';' is not a command. It should probably be commas.

Related

How can I print the ascii value of an input in Brainfuck?

What I want to do is for a Brainfuck code to print out the ascii value of the input. For example, typing in an input of "a" will give an output of 97. The python equivalent of this is print(ord(input())). What I'm thinking is that once I get the input with the , command, I can split the input value's digits into separate cells, and then print each cell individually. What I mean by this is let's say you type in an input of a. The , command will store the ascii value of a in the first cell(cell 0), which is 97 in this case. Then I run some algorithm that will split the 97 into its individual digits. So, in this case, cell 1 will have a value of 0(because 97 has a hundred digit of 0), cell 2 will have a value of 9, and cell 3 will have a value of 7. Then we can add 48 to each of those cells(0 has an ascii value of 48) and print each cell individually, starting from cell 1(the hundreds place). The problem I'm facing is writing the digit separation algorithm. I can't seem to make it work. My idea is to subtract 100 from the original number until that number is less than 100 while keeping track of how many times 100 has been subtracted, then repeatedly subtract 10, and finally we are left with the ones place. But the problem with this idea is that I have no idea how to track if the number falls under 100 or 10. Any suggestions or ideas? Thanks for the help in advance.
What you are trying to implement is called "divmod". divmod is a function that divides two numbers (in your case positive integers) and stores the result and the remainder. Implementations for this in brainfuck exist: Divmod algorithm in brainfuck
Good luck!

How can I duplicate a cell value to another cell in Brainfuck?

What I want to do is to first ask for an input for the value of cell 0(the first cell), then duplicate that input into the next cell(cell 1) while retaining the input value on cell 0. For example, if I typed in an input of 1, I would expect both cell 0 and cell 1 to have the value of 49(the ascii decimal value of 1 is 49. Check http://www.asciitable.com/ for other ascii values). My idea is to first ask for an input using the , command at cell 0, then duplicating that value to cell 1 and 2 using this code
[>+>+<<-](but cell 0 becomes 0 after this loop), then moving the value of cell 2 back to cell 0 using this code >>[<<+>>-](I need the two > at the beginning to move the pointer back to cell 2). I would like to know if there's a faster/more efficient way of doing this. Thanks!
The code im using so far to do this:
,[>+>+<<-]>>[<<+>>-]
Nope, there isn't. That's the most efficient way to do it. Brainfuck just isn't a very expressive language.

Matlab function NNZ, numerical zero

I am working on a code in Least Square Non Negative solution recovery context on Matlab, and I need (with no more details because it's not that important for this question) to know the number of non zero elements in my matrices and arrays.
The function NNZ on matlab does exactly what I want, but it happens that I need more information about what Matlab thinks of a "zero element", it could be 0 itself, or the numerical zero like 1e-16 or less.
Does anybody has this information about the NNZ function, cause I couldn't get the original script
Thanks.
PS : I am not an expert on Matlab, so accept my apologies if it's a really simple task.
I tried "open nnz", on Matlab but I only get a small script of commented code lines...
Since nnz counts everything that isn't an exact zero (i.e. 1e-100 is non-zero), you just have to apply a relational operator to your data first to find how many values exceed some tolerance around zero. For a matrix A:
n = nnz(abs(A) > 1e-16);
Also, this discussion of floating-point comparison might be of interest to you.
You can add in a tolerance by doing something like:
nnz(abs(myarray)>tol);
This will create a binary array that is 1 when abs(myarray)>tol and 0 otherwise and then count the number of non-zero entries.

if greater than matlab

I have a matrix of two columns and six rows, and want to build a second one with the following code:
for i=2
if F(:,i)<50
G(:,i) = 1
end
end
But nothing happens...
The idea was that if a value in the second column in F was less than 50, then the corresponding value in G would be 1.
Sorry for probably basic question, but no idea why this doesn't work. If I change to evaluate whether the F value ~= 50, then everything works as it should.
Thanks for any help.
Your if statement is only executed once - not once per element. While F(:,i)<50 returns an array of values, the if is either true or false; consequently, the next line is only executed once (either on all elements in G(:,i), or none of them).
For example, see this piece of code:
if(1 < [0 1 2]), disp('true'); end
It will produce no output, even though it is true for the third element. On the other hand,
if(1 < [2 3 4]), disp('true'); end
does produce output...
In general, the following:
1 < [0 1 2]
produces
0 0 1
Not sure why you say it doesn't work for < but it does work for ~=. Maybe there are no elements equal to 50, so it only "seems" to work?
In general, there is a better way to do what you want, with a single line:
G(F(:,2)<50,2)=1
This uses "logical indexing", and is much faster than looping. It will consider each element of F(:,2), and modify the corresponding element in G.
One final comment: it is not great practice to use the variable i since it has a built in value of sqrt(-1). If you have code anywhere that relies on it having that value, then accidentally overwriting it with any other value would break that. It's not the problem with your code today - but why set yourself up for a problem in the future.

Why do we have "is not a Number" (isNan) functions?

Many languages have an isNaN() function. I am asking myself: why check for not being a number?
Is the reason purely logical or is it faster to check for not a number instead of is a number?
Note that this is a pure question of understanding. I know that I can negate isNaN() to achieve an isNumber() function for example.
However I am searching for a reason WHY we are checking for not a number?
In computing, NaN (Not a Number) is a
value of numeric data type
representing an undefined or
unrepresentable value, especially in
floating-point calculations.
Wiki Article
Because Not a Number is a special case of an expression.
You can't just use 0 or -1 or something like that because those numbers already have meanings.
Not a Number means something went awry in a calculation and a valid number cannot be computed out of it.
It's on the same line of thinking as having null. Sure, we could assign an arbitrary numerical value to mean null but it would be confusing and we'd hit all sorts of weird errors on corner cases.
'Not a Number' is the result of specific floating point calculations. It's not about "hey, is this variable holding 120 or "abc"?'
isThisCaseExceptional seems more reasonable to me than isEverythingNormal because I'm likely to write
possible_number = some_calculation();
if (inNaN(possible_number)) handle_the_surprise;
// .. keep going
instead of
possible_number = some_calculation();
if (inANumber(possible_number)) {
// .. keep going
} else {
// handle the surprise
}
The check is for whether it is not a number, because the assumption is that it is a number. NaN is the exceptional case when you're expecting a numeric value, so it makes sense to me that it is done this way. I'd rather check for isNaN infrequently than check if a value isNum frequently, after all.
It is a way to report an error condition (mathematically undefined or outside of technical limits).
Many languages have special representations if the result of a computation is not representable by a number, for example NaN and Inf. So isNaN() checks, if a result is actually a number or the special marker for NaN.
NaNs are used for:
Nonreal or undefined values.
Error handling. Initializing a variable with NaN allows you to test for NaN and make sure it has been set with a valid value.
Objects that ignore a member or property. For example, WPF uses NaN to represent the Width and Height of visual elements that do not define their own size.