I am using Microchip XC8 and am getting a bit confused.
I have that code:
void fastBinaryToBCD(unsigned short int n, unsigned char mem){
// AppNote www.cypress.com/file/42131
unsigned char d4, d3, d2, d1, d0, q; // d4-d0 - decimal numbers
d0 = n & 0xF;
d1 = (n>>4u) & 0xF;
d2 = (n>>8u) & 0xF;
d3 = (n>>12u) & 0xF;
d0 = 6u * (d3 + d2 + d1) + d0; //<------------warning
q = d0 / 10u;
d0 = d0 % 10u;
d1 = q + 9u*d3 + 5u*d2 + d1;
//...
}
I am getting a warning:
warning: (373) implicit signed to unsigned conversion
I recon that all literals are signed by default, so i put all the u's. It works everywhere, except for that line I pointed out.
What also doesn't yield a non-warning result is:
d0 = (unsinged char)(6u * (d3 + d2 + d1) + d0);
The warning remains.
What's going on in this line? :D
Best regards!
Sometines xc8 is a little bit tricky with warnings.
The literal 0x0F is signed so please try:
d0 = n & 0xFu;
d1 = (n>>4u) & 0xFu;
d2 = (n>>8u) & 0xFu;
d3 = (n>>12u) & 0xFu;
Related
I'm trying to implement robust / stable Laguerre's method. My code works for most of polynomials but for few it "fails". Respectively I don't know how to properly handle "corner" cases.
Following code tries to find single root (F64 - 64bit float, C64 - 64bit complex):
private static C64 GetSingle( C64 guess, int degree, C64 [] coeffs )
{
var i = 0;
var x = guess;
var n = (F64) degree;
while( true )
{
++Iters;
var v0 = PolyUtils.Evaluate( x, coeffs, degree );
if( v0.Abs() <= ACCURACY )
break;
var v1 = PolyUtils.EvaluateDeriv1( x, coeffs, degree );
var v2 = PolyUtils.EvaluateDeriv2( x, coeffs, degree );
var g = v1 / v0;
var gg = g * g;
var h = gg - ( v2 / v0 );
var f = C64.Sqrt(( n - 1.0 ) * ( n * h - gg ));
var d0 = g - f;
var d1 = g + f;
var dx = d0.Abs() >= d1.Abs() ? ( n / d0 ) : ( n / d1 );
x -= dx;
// if( dx.Abs() <= ACCURACY )
// break;
if( ++i == ITERS_PER_ROOT ) // even after trying all guesses we didn't converted to the root (within given accuracy)
break;
if(( i & ( ITERS_PER_GUESS - 1 )) == 0 ) // didn't converge yet --> restart with different guess
{
x = GUESSES[ i / ITERS_PER_GUESS ];
}
}
return x;
}
At the end if it didn't found root it tries different guess, first quess (if not specified) is always 'zero'.
For example for 'x^4 + x^3 + x + 1' it founds 1st root '-1'.
Deflates (divides) original poly by 'x + 1' so 2nd root search continues with polynomial 'x^3 + 1'.
Again it starts with 'zero' as initial guess... but now both 1st and 2nd derivates are 'zero' which leads to 'zero' in 'd0' and 'd1'... ending by division-by-zero (and NaNs in root).
Another such example is 'x^5 - 1' - while searching for 1st root we again ends with zero derivates.
Can someone tell me how to handle such situations?
Should I just try another guess if derivates are 'zero'? I saw many implementation on net but none
had such conditions so I don't know if I'm missing something.
Thank you
I am reading "Numerical Methods for Engineers" by Chapra and Canale. In it, they've provided pseudocode for the implementation of Euler's method (for solving ordinary differential equations). Here is the pseucode:
Pseucode for implementing Euler's method
I tried implementing this code in GNU Octave, but depending on the input values, I am getting one of two errors:
The program doesn't give any output at all. I have to press 'Ctrl + C' in order to break execution.
The program gives this message:
error: 'ynew' undefined near line 5 column 21
error: called from
Integrator at line 5 column 9
main at line 18 column 7
I would be very grateful if you could get this program to work for me. I am actually an amateur in GNU Octave. Thank you.
Edit 1: Here is my code. For main.m:
%prompt user
y = input('Initial value of y:');
xi = input('Initial value of x:');
xf = input('Final value of x:');
dx = input('Step size:');
xout = input('Output interval:');
x = xi;
m = 0;
xpm = x;
ypm = y;
while(1)
xend = x + xout;
if xend > xf
xend = xf;
h = dx;
Integrator(x,y,h,xend);
m = m + 1;
xpm = x;
ypm = y;
if x >= xf
break;
endif
endif
end
For Integrator.m:
function Integrator(x,y,h,xend)
while(1)
if xend - x < h
h = xend - x;
Euler(x,y,h,ynew);
y = ynew;
if x >= xend
break;
endif
endif
end
endfunction
For Euler.m:
function Euler(x,y,h,ynew)
Derivs(x,y,dydx);
ynew = y + dydx * h;
x = x + h;
endfunction
For Derivs.m:
function Derivs(x,y,dydx)
dydx = -2 * x^3 + 12 * x^2 - 20 * x + 8.5;
endfunction
Edit 2: I shoud mention that the differential equation which Chapra and Canale have given as an example is:
y'(x) = -2 * x^3 + 12 * x^2 - 20 * x + 8.5
That is why the 'Derivs.m' script shows dydx to be this particular polynomial.
Here is my final code. It has four different M-files:
main.m
%prompt the user
y = input('Initial value of y:');
x = input('Initial value of x:');
xf = input('Final value of x:');
dx = input('Step size:');
xout = dx;
%boring calculations
m = 1;
xp = [x];
yp = [y];
while x < xf
[x,y] = Integrator(x,y,dx,min(xf, x+xout));
m = m+1;
xp(m) = x;
yp(m) = y;
end
%plot the final result
plot(xp,yp);
title('Solution using Euler Method');
ylabel('Dependent variable (y)');
xlabel('Independent variable (x)');
grid on;
Integrator.m
%This function takes in 4 inputs (x,y,h,xend) and returns 2 outputs [x,y]
function [x,y] = Integrator(x,y,h,xend)
while x < xend
h = min(h, xend-x);
[x,y] = Euler(x,y,h);
end
endfunction
Euler.m
%This function takes in 3 inputs (x,y,h) and returns 2 outputs [x,ynew]
function [x,ynew] = Euler(x,y,h)
dydx = Derivs(x,y);
ynew = y + dydx * h;
x = x + h;
endfunction
Derivs.m
%This function takes in 2 inputs (x,y) and returns 1 output [dydx]
function [dydx] = Derivs(x,y)
dydx = -2 * x^3 + 12 * x^2 - 20 * x + 8.5;
endfunction
Your functions should look like
function [x, y] = Integrator(x,y,h,xend)
while x < xend
h = min(h, xend-x)
[x,y] = Euler(x,y,h);
end%while
end%function
as an example. Depending on what you want to do with the result, your main loop might need to collect all the results from the single steps. One variant for that is
m = 1;
xp = [x];
yp = [y];
while x < xf
[x,y] = Integrator(x,y,dx,min(xf, x+xout));
m = m+1;
xp(m) = x;
yp(m) = y;
end%while
I've installed and loaded the symbolic package that becomes available from optim package to obtain the syms function (like in MATLAB) but when I use solve() function the command window goes in Waiting mode like
Waiting..........
My code is given below:
syms s T K D1 D2 D3 theta1 theta2 theta3 J1 J2 J3
eq1 = (s * D1 + K + J1 * s ^ 2)* theta1 - K * theta2 == T;
eq2 = -K * theta1 + (J2 * s ^ 2 + K + D2 * s) * theta2 - D2 * s * theta3 == 0;
eq3 = -D2 * s * theta2 + (D3 * s + J3 * s ^ 2 + D2 * s) * theta3 == 0;
S = solve(eq1, eq2, eq3)
but if I manually solve it by inverse method, it gives the answer instantly. Kindly help to solve this bug.
I'm a beginner in C++ and i have a problem that i dont know how to solve it,
I have an int function that few parameters should be return on it:
int sphere(const float & X,const float & Y,const float & Z,
const int & Px, const int & Py, const int & Pz,
const int & diameterOfSphere, const int & number)
{
return pow(Px-X,2) + pow(Py+(diameterOfSphere * (number - 1))-Y,2)
+ pow(Pz-Z,2) <= pow(diameterOfSphere/2,2);
}
in this function, the integer "number" may should be start from 2 to for example 100. I need to do something that if i choose 100 for "number", the return statement should be repeated 99 times and separated by a plus ( + ).
for example i can do it manually but it is needed to write a lot of codes which is not logical
for example, i did it manually for just three times
return (pow(Px-X,2)+pow((Py+(diameterOfSphere * 2))-Y,2)+pow(Pz-Z,2)
<= pow(diameterOfSphere/2,2))
+ (pow(Px-X,2)+pow((Py+(diameterOfSphere * 3))-Y,2)+pow(Pz-Z,2)
<= pow(diameterOfSphere/2,2))
+ (pow(Px-X,2)+pow((Py+(diameterOfSphere * 4))-Y,2)+pow(Pz-Z,2)
<= pow(diameterOfSphere/2,2))
+ (pow(Px-X,2)+pow((Py+(diameterOfSphere * 5))-Y,2)+pow(Pz-Z,2)
<= pow(diameterOfSphere/2,2)) ;
Is there any easier way? I know i have to use a loop but i dont know how to do it in this case
Thanks a lot
Don't use pow() to do spheres squares, pow() is an exponential function that is quite slow. Break your formula and format your lines to make code readable. Your point's coordinates are integer, is that intentional? This variant is not only more readable, it's more likely to be optimized by compiler:
int sphere(const float & X,const float & Y, const float & Z,
const int & Px, const int & Py, const int & Pz,
const int & diameterOfSphere, const int & number)
{
const float dx = Px - X;
const float dy = Py + diameterOfSphere * (number - 1) - Y;
const float dz = Pz - Z;
const float D = dx*dx + dy*dy + dz*dz;
return D <= 0.25 * diameterOfSphere*diameterOfSphere;
}
Now if I understood you right, you need a recursion or a loop that emulates recursion. You actually can call function from itself, do you know that?
int sphere(const float & X,const float & Y, const float & Z,
const int & Px, const int & Py, const int & Pz,
const int & diameterOfSphere, const int & number)
{
const float dx = Px - X;
const float dy = Py + diameterOfSphere * (number - 1) - Y;
const float dz = Pz - Z;
const float D = dx*dx + dy*dy + dz*dz;
if(!(number>0))
return 0;
return D <= 0.25 * diameterOfSphere*diameterOfSphere
+ sphere(X,Y,Z,Px,Py,Pz,diameterOfSphere, number -1);
}
Negative side of recursion a) each function call fills stack with variables and parameters stored b) there is an extra call that returns immediately.
Py + diameterOfSphere * (number - 1) - Y expression throws me back, is that a mistake? Pretty much it almost never would cause comparison to be true. And it's still not clear what you're trying to do with those comparisons. So, while I modified code so it would be equal to your idea, it looks chaotic\senseless. The >= or <= would return 1 or 0 as result. Or did you mean this?
return ( D <= 0.25 * diameterOfSphere*diameterOfSphere )
+ sphere(X,Y,Z,Px,Py,Pz,diameterOfSphere, number -1);
The following code is meant to output a 1 in the case of wires S1 and X being asserted and wire S0 being deasserted. However, when I run the wave form, the output is constantly 0.
The logic equations governing the wires are:
S1 = (S0 & ~X) | (S1 & ~S0 & X)
S0 = X
O = (S1 & S0)
Is there a problem with my code:
module Dff1(D, clk, Q, Qbar);
input D, clk;
output reg Q;
output Qbar;
initial begin
Q = 0;
end
assign Qbar = ~Q;
always #(posedge clk)
Q = D;
endmodule
module Mod1 (clk, X, O);
input clk, X;
output O;
wire S1, S0, Q1, Q0, Q1bar, Q0bar;
assign S1 = (S0 & ~X) | (S1 & ~S0 & X);
Dff1 C1(S1, clk, Q1, Q1bar);
assign S0 = X;
Dff1 C0(S0, clk, Q0, Q0bar);
assign O = (S1 & S0);
endmodule
module test_bench ();
wire clk;
reg osc;
reg [1:0] R;
reg Seqinput;
integer num;
initial begin
osc = 0;
num = 0;
Seqinput = 0;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars;
#20000 $finish;
end
always begin
#10 osc = ~osc;
num = (num >= 7) // counter incremented by 1 from 0..7
? 0 : (num + 1);
if ((num % 2) == 0) begin // every other time step
R = $random % 2; // $random generates a 32-bit signed
// random number
// -1 <= $random % 2 <= 1
if (R > 0)
Seqinput = 1; // input is 1
else
Seqinput = 0; // input is 0
end
end
assign clk=osc;
wire Out1;
Mod1 Mod1instance(clk, Seqinput, Out1);
endmodule
Explained with substitution:
S1 = (S0 & ~X) | (S1 & ~S0 & X) sub S0 with X
S1 = ((X) & ~X) | (S1 & ~(X) & X) X & ~X == 0
S1 = ( 0 ) | ( S1 & 0 ) S1 & 0 == 0;
S1 = ( 0 ) | ( 0 )
S1 = 0
Since the assignment of S1 dependent on its current value, it is considered asynchronous feedback logic. This is normally something you don't want to do. I believe the real equation you want is:
S1 = (Q0 & ~X) | (Q1 & ~Q0 & X)
This makes the code synchronous and predictable. Q1 and Q0 are the previous clocked values of S1 and S0 respectively.
Also, it is important to use non-blocking assignments when assigning (<=) flops. Verilog is a non-determent simulator. This means operations scheduled in the same region can happen in any order. Using non-blocking on a flop moves the assignment to the NBA region while its evaluation in kept in the active region.
always #(posedge clk)
Q <= D;