I need help in adding logic to the following code to handle divide by zero
=iif(Fields!COVERAGE.value = 0, Sum(Fields!CalculatedTotalIncidents.Value) / (ReportItems!CalculatedUnitsSold1.value), Sum(Fields!CalculatedTotalIncidents.Value) / Sum(Fields!CalculatedUnitsSold.Value))
It should be very simple if you implement a function in custom code of report
function div (a as integer, b as integer) as decimal
if (a > 0 ) then
div = a/b
else
div = 0
end if
end function
Then call this function in your report. You also can extend this function base on your specific business rule.
Hope this help.
Try this:
=iif(Fields!COVERAGE.value = 0,
Sum(Fields!CalculatedTotalIncidents.Value)/
iif((ReportItems!CalculatedUnitsSold1.value)<>0,
(ReportItems!CalculatedUnitsSold1.value),1),
Sum(Fields!CalculatedTotalIncidents.Value) /
iif(Sum(Fields!CalculatedUnitsSold.Value)<>0,
Sum(Fields!CalculatedUnitsSold.Value),1))
Related
How should I handle divide by zero error in rdl expression for SSRS 2017?
I have tried the below approaches but nothing works.
1.
=iif((iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)))=0,0,
SUM(Fields!ClosingStock.Value)/(iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30))))*30)
(SUM(Fields!ClosingStock.Value)/
(iif((SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)))*30)
You should only be checking the denominator for zero - not the division operation.
=IIF(Fields!daysINDate.Value = 0, 0, SUM(Fields!Consumption.Value))
/
IIF(Fields!daysINDate.Value = 0, 1, Fields!daysINDate.Value)
*30
SSRS checks both sides of the IIF statement... if either the THEN or ELSE part if the IIF is 0, it will throw up an error.
One way to get around it is to use VB code.
Right click on your report -> report properties -> code and paste the following code :
Public Function NDZ(Byval a As Decimal,Byval b As Decimal, Byval c As Decimal) As Decimal
' Fix for divide by zero problem in VB
' calculates a/b and either returns result or c if b = 0
if b = 0 then
return c
else
return a/b
end if
end function
Now on your report, enter the following expression in your text box you want to perform the calculation :
=code.NDZ(SUM(Fields!Consumption.Value), Fields!daysINDate.Value, 0)
so if either Fields!daysINDate.Value or SUM(Fields!Consumption.Value) is 0, then it will return 0 and not an error.
I am trying a simple function as follows:
function out=Y_T(f,a,b)
Y_T=f(a)-f(b)
end
f is an argument which is a function itself. For example f=x^4+3. The function T_Y should evaluate the values of f in 'a' and 'b' and subtract them. But when i try to use this function for example T_Y(x^4+3,5,2) i face with an error: Index exceeds matrix dimension. How can i fix it? Any tips will be appreciated.
Thanks a lot.
I think you simply have your syntax wrong - as written, you're passing the numeric value x^4 + 3 into Y_T. I think what you need is:
Y_T(#(x) x^4 + 3, 5, 2)
This defines an anonymous function, and passes it in to Y_T.
Your definition of Y_T is slightly wrong too - you need to assign the result to out, like so:
function out=Y_T(f,a,b)
out=f(a)-f(b)
end
I am getting an #ERROR in SSRS. I believe it's happening when it tries to divide 0 by 0.
How can I change this to handle that scenario.
=Fields!Total_Incidents.Value/Fields!Units_Sold.Value
You can add a function in custom code of the report, ex:
function divide(a as decimal, b as decimal) as decimal
if (b > 0.0) then
divide = a/b
else
divide = 0
end if
end function
Then, call this function in your textbox expression
This was how I ended up doing it
=IIf(Fields!Units_Sold.Value = 0, 0, Fields!Total_Incidents.Value / IIf(Fields!Units_Sold.Value = 0, 1, Fields!Units_Sold.Value))
Check denominator is zero or not,
=IFF(Fields!Units_Sold.Value=0,0,Fields!Total_Incidents.Value/Fields!Units_Sold.Value)
As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points
I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.