Struggle with Binary Substraction with using two's complement - math

I have to solve a binary substraction of the following exercise:
-6 + 4.5
We know that -6 + 4.5 = 4.5 - 6;
As we know, to perform the subtraction of signed numbers (M – N) we should find the 2’s complement of the N , and add it to M.
4.5 = 0100.1
6 = 0110; 2's complement of 6 is 1010
Now addition:
0100.1 + 1010 = 1110.1
The rule says, if the summation does not produce an end carry,
the leftmost bit is the sign bit.
1110.1 = -6.5
But the result should be -1.5
The question is: Where is my mistake?

Related

How to properly fit a non-linear equation to a set of datapoints?

I have a curve of net longwave radiation (QL) data, which are calculated as follows:
QL = a*Ta^4 - b*Ts^4
where a and b are constants, Ta is the air temperature and Ts is the surface temperature
If I plot a curve of QL versus Ta-Ts, what type of equation should I use to fit the data as follows y = f(x) where x = (Ta-Ts)?
Thanks
-20.5 -176.683672
-19.5 -171.0655836
-18.5 -165.8706233
-17.5 -158.9990897
-16.5 -154.2715535
-15.5 -147.5376901
-14.5 -141.2410818
-13.5 -135.3387669
-12.5 -129.3971791
-11.5 -122.0777208
-10.5 -117.475907
-9.5 -111.107148
-8.5 -104.5999237
-7.5 -99.82769298
-6.5 -93.43215832
-5.5 -87.6278432
-4.5 -81.85415752
-3.5 -76.5997892
-2.5 -70.26308516
-1.5 -65.49437303
-0.5 -60.78052134
0.5 -56.32077454
1.5 -51.74037492
2.5 -47.30542394
3.5 -42.92298839
4.5 -38.13260904
5.5 -34.22676827
6.5 -30.49502686
7.5 -26.89383663
8.5 -22.259631
The complete data https://docs.google.com/spreadsheets/d/1e3gNCKQesrGe9ESrEIUcQw3umERzNRt0/edit?usp=sharing&ouid=115727378140347660572&rtpof=true&sd=true:
TS = surface temperature (degrees Celsius);
TA = air temperature (degrees Celsius);
Lin = longwave in (0.8 * 5.67E-8 * (TA+273.15)^4) (W m-2);
Lout = longwave out (0.97 * 5.67E-8 * (TS+273.15)^4) (W m-2);
QL = Lin - Lout (W m-2);
The notation QL=y=f(x) is falacious because QL doesn't depends on one variable only, but depends two independant variables Ta and Ts.
So, one have to write : y=F(Ta,Ts) or equivalently y=g(x,Ta) or equivalently y=h(x,Ts) with x=Ta-Ts and the functions F or g or h.
Any one of those functions can be determined thanks to nonlinear regression if we have data on the form of a table of three columns (not only two columns) for example :
(Ta,Ts,y) to find the function F(Ta,Ts)
or (x,Ta,y) to find the function g(x,Ta)
or (x,Ts,y) to find the function h(x,Ts)
In fact one cannot definitively answer to your question in which something is missing : Either measurements of another parameter or another relationship between the parameters in addition to the relationship x=Ta-Ts.
Of course one can compute (for example) the coefficients A,B,C,... for a polynomial regression of the kind f(x)=A+Bx+Cx^2+... and get a very good fitting :
The coefficients A,B,C are purely mathematical without physical signifiance. The coefficients a and b in f(x)=aTa^4+bTs^4 cannot be derived from the coefficients A,B,C without more physical information as already pointed out.
I took your data and did a 4th order polynomial fit. Here's the result:
QL = -58.607 + x*(4.8336 + x*(-0.0772 + x*(-2e-5 + x*8e-5)))
R^ = 0.9999
x = (Ta - Ts)
If you want the equation to be in terms of Ta and Ts instead of the difference you should substitute and do the algebra.

how to get nth term of series 2,3,3,4,4,4,5,5,5,5,6,6,6,6,6,

I want to get nth term of the sequence of binary numbers with only two set bits in increasing order
(11,101,110,1001,1010,1100,10001,10010,10100,11000....).
I figured out it can be formed with string manipulation if I know the nth term of this series below:
(2,3,3,4,4,4,5,5,5,5,6,6,6,6,6.....).
Can someone help me with how to get nth term of series:
(2,3,3,4,4,4,5,5,5,5,6,6,6,6,6.....)
Edit: please focus to get the nth term of series only (2,3,3,4,4,4,5,5,5,5,6,6,6,6,6.....). I have other parts figured out.
This is series A003057 at oeis.org.
There are several formulae that can be used to calculate the nth value. The simplest appears to be as follows:
a(n) = ceiling((sqrt(8n-7)+1)/2)
There are C(N,1)=N combinations of length N, starting with 1 and containing 2 ones.
So you have one 2, two 3s, three fours and so on - note arithmetic progression. Another point of view: we can see the first
2: at the 1st place
3: 2nd
4: 4th
5: 7
6: 11
k: (k-1)*(k-2)/2 + 1 //from arithmetic progression sum formula
So solve quadratic equation
N = (k-1) * (k-2) / 2 + 1
or
k^2 - 3 * k + 4 - 2 * N = 0
for unknown k - find positive root floored down to integer and you will get k as number at the n-th place in sequence
D = 9 - 16 + 8 * N = 8*N - 7
k = Floor((3 + Sqrt(8*N - 7)) / 2)
example: for N=7 k = (3+Sqrt(56-7))/2 = 5
for N=10 k = Floor(3+Sqrt(80-7))/2 = Floor(5.77) = 5

Calculating powers modulo n

Is there in R a function allowing to fast calculate powers modulo n ? For example, suppose I want to calculate :
(10 378)^8743 (mod 10403)
I know it's possible to use successives powers of 2 by writing :
8743 = 8192 + 512 + 32 + 4 + 2 + 1
But is something already implemented ?
I believe the modpower function in the numbers package is what you want.

Linear Congruences

Part A:
For the two systems of linear congruences, one system has integer solutions while the other does not. For the system with integer solutions, write down 2 of them whose difference is less than 192. For the other system, explain why no integer solution exists.
A: n congruent 13 (mod 16)
n congruent 5 (mod 12)
B: n congruent 14 (mod 16)
n congreunt 4 (mod 12)
Part B:
Let a1 and a2 be integers.
Let m1 and m2 be natural numbers.
Let d = gcd(m1,m2)
Based on your observations from part A, complete the following proposition and prove it.
Proposition1: The system:
n congruent a1 (mod m1)
n congruent a2 (mod m2)
has an integer solution if and only if ____________
(The blank needs to be filled with a simple condition on a1,a2,d)
Any tips would be great! Thanks in advance!
16 and 12 are not coprime, so the usual Chinese Remainder Theorem doesn't apply. In fact, since they have a common factor of 4, that means that the system only has a solution when (x mod 16) and (x mod 12) are congruent mod 4. As you can see, this is only true in one of the systems listed above.
In the case where there is a solution, the minimal distance between solutions is lcm(16,12)=48 rather than 16*12 = 192 because of the common factor in the moduli. The CRT ensures that knowing x mod 12 is equivalent to knowing x mod 3 and x mod 4 since 12=3*4 and 3 and 4 are coprime. However, you already know x mod 4 because you know x mod 16 and 4 divides 16. So you can think of the extra information provided by the second equation as only being x mod 3 rather than x mod 12.
I hope this helps. I'm not sure how to explain it better without just giving the answer away.

How to Convert from a Residual Number System to a Mixed Radix System?

I understand the concept of a Residual Number System and the concept of a Mixed Radix system, but I'm having difficulty getting any of the conversion methods I find to work in a simple case study.
I started at Knuth's Art of Computer Programming but that had a bit too much on the theory of the conversion, and once Euler was mentioned I was lost. Wikipedia has a nice section on the subject, which I tried here and here but both times I couldn't get back to the number where I started.
I found a good article here (PDF), which I condensed the relevant sections here, but I don't understand the multiplicative inverses and their notation. Specifically, how y_2 = |(3 - 19)|(1/31)|_7|_7 = |5 * 5|_7 Especially how |1/31|_7 = 5
The multiplicative inverses are to be taken with respect to a modulus (here 7). Since the modulus 7 is prime, every number (modulo 7) has an inverse. In particular, 31_7 = 3_7 (since 31 = 4*7 +3 - sorry if I'm too didactic), and its inverse is 5 because 3 * 5 = 15 = 1_7. So we can write
|1/31|_7 = 5.
Now
y_2 = |(3 - 19) |(1/31)|_7 |_7
= | (-16) * 5 |_7
= | 5 * 5 |_7 since -16 = (-3)*7 + 5
= 4

Resources