decrement operator vs subtraction operator [duplicate] - subtraction

This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 7 years ago.
If I write a code like(in c)
x=1;
z=2;
y=x---z;
will first two - be treated as post-decrement and later one as subtraction
or first - will be treated as subtraction and other two as pre-decrement
and what if I put a space to make it the other (because in c program doesn't change by white space)

As per the C11 standard, chapter ยง6.4 , lexical elements, (emphasis mine)
If the input stream has been parsed into preprocessing tokens up to a given character, the
next preprocessing token is the longest sequence of characters that could constitute a
preprocessing token. [..]
So,
y=x---z;
is
y= (x--) - z;
This is also called as Maximal munch rule.

Related

How to circumvent the extremely small output values? [duplicate]

This question already has an answer here:
Is there any way to increase 'realmax' in MATLAB?
(1 answer)
Closed 7 years ago.
For example in Matlab:
log(exp(-200)
-200
however
log(exp(-2000))
-inf
Naturally the input to the log function is passed as zero as exp(-2000) is insignificant and the log gives -inf. How can I fix this to receive -2000 instead?
If you have the Symbolic Math Toolbox, it's possible to do this with Variable Precision Arithmetic. Use the vpa function and place your mathematical expression as input into vpa wrapped in a string:
>> vpa('log(exp(-2000))')
ans =
-2000.0
However, this will be represented in symbolic format, so it may be prudent to convert back to a numerical value after you're done. Convert this result using double once you perform the calculation:
>> double(vpa('log(exp(-2000))'))
ans =
-2000
It is often possible to rewrite formulas in a way they do not exceed the range of floating point values. In your case, rewriting it would be trivially -2000. A more "real world" example can be found in this question where rewriting successfully avoided the problem.

How to access matrix alphabetically [duplicate]

This question already has answers here:
How to compute word scores in Scrabble using MATLAB
(2 answers)
Closed 7 years ago.
I'm new to matlab ... I simply would like to convert letter to numbers such that:
A=1
B=2
C=3
all my numbers are capital case. Off course, I could define constant for each char, but is there a shorter way?
Thanks!
See the char function. You can give it an integer argument.
http://www.mathworks.com/help/matlab/ref/char.html

why I cannot get just a number [duplicate]

This question already has answers here:
Convert output from symbolic math (sym) to float
(2 answers)
Closed 8 years ago.
I did following:
clc
clear all
I0=1.2e12;
FWHM=10e-12;
c=FWHM./2.35482;
t=0:1e-12:50e-12;
syms t
int(I0.*exp(-1.*(t-5e-12).^2./(2.*c.^2)),t,0,40e-12)
but it does not give me a simple number (just a number)
The reason why matlab does not automatically give you a number is that precision could be lost.
Suppose you have a symbolic variable with value 1/3. That has infinite accuracy at this point. Yet if you evaluate it, you would lose this precision, so that is why it is not evaluated directly.
If you want to evaluate it, you could do that of course. Try doc double,doc vpa, doc eval or doc subs. I think the first one is what you need.

Subscript indices must either be real positive integers or logicals ERROR [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 9 years ago.
I have this strange error 'Subscript indices must either be real positive integers or logicals' which most of the times pops up. There a few times though that it doesn't. My code is kind of huge and it has to do with calculating the voronoi diagram without using the voronoi function of MATLAB. The error occurs in one of the bellow code parts each time:
if (PossibleVoronoiPoints(m,2)-Slope(k)*PossibleVoronoiPoints(m,1)-c(k)>0)
or
if (PossibleVoronoiPoints(n,2)-Slope(k)*PossibleVoronoiPoints(n,1)-c(k)<0)
Can anyone help me understand what's going on? If you need the whole code i'll post it with comments if necessary.
This means what it means: one (or more) of the following subscripts: k, m, or n contains an invalid value. To overcome this error, you need to make sure that each subscript is valid, that it is either a positive integer or a logical (boolean) value (true or false).
If you're not properly familiar with matrix indexing in MATLAB, I suggest that you read this article or see this answer.

How to add two one-digit numbers in BF [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the sum of 2 numbers with BrainFuck
Do anyone know how to write a simple BF program that adds two one-digit numbers? I am new to the language and need some help to grab the concepts.
If you have two cells having a value of 0 to 9 each you can just add one to the other. Suppose you have two cells A and B. A is at position 0 and B is at position 1. You can add B to A like this (Assume the pointer starts at A). I will set A to 4, B to 8 and then add B to A:
setting A and B
++++>++++++++
remember the pointer is at B now so we can add B to A like this
[<+>-]
and now the pointer is still at B but B contains 0 and A contains 12
If you want to have the user input those single digit numbers, then keep in mind that when you use a , character, the ASCII code of the character is put in the current cell. So you first need to subtract 48 from the number (48 is the ASCII code of the character '0'). Here is an example filling A and B with two characters from the keyboard (I will assume that the user ONLY presses any of the number keys, and not letters or symbols)
Pointer starts at A so we have the user press a number key
,
we then subtract 48 from it so that it contains the actual value
------------------------------------------------
we move to B and do the same
>,------------------------------------------------
from here on it's the same as the last example
[<+>-]