why condition become true in if/esle ?

1 Ansicht (letzte 30 Tage)
Sarfaraz Ahmed
Sarfaraz Ahmed am 13 Nov. 2018
Kommentiert: Sarfaraz Ahmed am 13 Nov. 2018
Hi, the below simple code is making trouble that : here Vxp and Vxn both are same value(32.000); so the difference must be 0 but here program still enter in if condition. instead it should enter in else condition. why it's happening ? anyone have the idea what cause make condition false ? even I check values by insterting break point. however same code in another matlab function block is running fine. could anyone help in this regard ?
for kbit = 1:Nbit
if Vxp - Vxn > 0
B(kbit) = 1;
Vxp = Vxp - Vref*2^(-kbit);
else
B(kbit) = 0;
Vxn = Vxn - Vref*2^(-kbit);
end
end
Thanks

Akzeptierte Antwort

madhan ravi
madhan ravi am 13 Nov. 2018
Bearbeitet: madhan ravi am 13 Nov. 2018
  6 Kommentare
Sarfaraz Ahmed
Sarfaraz Ahmed am 13 Nov. 2018
Thank you. I made typing mistake it basically (Vxp-Vxn= 1.42e-14) so I keep tolarance 1.42e-14. I only keep tolarance when both values are equal otherwise I have to omit this tolarance as it makes affect on further calculation in the loop.
madhan ravi
madhan ravi am 13 Nov. 2018
Anytime :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 13 Nov. 2018
Bearbeitet: Stephen23 am 13 Nov. 2018
"...here Vxp and Vxn both are same value(32.000); so the difference must be 0..."
Evidently not true.
"why it's happening ? anyone have the idea what cause make condition false ?"
Sure, read these:
This is worth reading as well:
All programmers need to understand that calculations on floating point numbers can accumulate floating point errors (like your example does), and do not expect floating point mathematics to be equivalent to the symbolic maths that they learned in high school (e.g. operations on floating point values are not commutative). They write their code accordingly, by comparing the difference of floating point values against a tolerance:
abs(A-B)<tol
  5 Kommentare
Stephen23
Stephen23 am 13 Nov. 2018
Bearbeitet: Stephen23 am 13 Nov. 2018
@Sarfaraz Ahmed: your code has a few interesting features, e.g.:
  • you have an Nbit variable to select the number of ADC bits, but then you hardcoded B(7), B(6), etc. So when I tried your code with Nbits=4, it threw an error.
  • you enlarge B on each loop iteration. It would be more efficient to preallocate this before the loop.
  • No code comments, no help, no input/outut specifications, not description, no named algorithm.
I am not completely sure what your algorithm is. Can you please give a link that explains the algorithm you are trying to implement.
Sarfaraz Ahmed
Sarfaraz Ahmed am 13 Nov. 2018
Thanks Stephen. Yes I can list here code inside that block : here Vin and Vip are different discrete values (sometime similar values) as I mentioned above. The problem is, same code is running in another blockB and that block is giving correct value when both values are similar. but bllockA is not giving correct value because it enters in if when both values are similar. if you say, I can give you my simulink design and you can check in the model by inserting breakpoint at the 2nd rising edge of clock.
%blockA
function [y,y1] = ADC(Vin, Vip)
coder.extrinsic('stem');
coder.extrinsic('get_param')
sim_t=get_param('ADC_Sign_E_ref_2','SimulationTime')
persistent simt;
if isempty(simt)
simt=0;
end
simt=sim_t;
Nbit = 7;
Vref = 64;
% generating empty plot
ax1=subplot(2,2,([3,4]));
ax2=subplot(2,2,([3,4]));
axis ([ax1 ax2], [0 0.2 -64 64]);
title('Dout Scaler Value');
xlabel('Time(s)');
ylabel('Amplitude');
hold on;
persistent Dout;
if isempty(Dout)
Dout = zeros(1,2);
end
persistent B;
if isempty(B)
B = zeros(1,Nbit);
end
% Conventional Set-and-Down SAR ADC
% 64C, 32C, 16C, 8C, 4C, 2C, C, C
Vxp = Vip;
Vxn = Vin;
if(Vxp-Vxn <=1.4211e-14)
x=-1;
else
x=Vxp-Vxn;
end
for kbit = 1:Nbit
if x > 0
B(kbit) = 1;
Vxp = Vxp - Vref*2^(-kbit);
else
B(kbit) = 0;
Vxn = Vxn - Vref*2^(-kbit);
end
x=Vxp-Vxn;
end
Dout = B(1)*64 + B(2)*32 + B(3)*16 + B(4)*8 + B(5)*4 + B(6)*2 + B(7)*1 -64 +0.5 ;
stem(sim_t,Dout);
hold on;
y = Dout;
y1=simt;
disp('test');
is this information is sufficient ? Thank you.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by