How to avoid sum-issues in stimulsoft - c#

I use 'Stimulsoft Reports.Net' to calculate a total price based on multiple items with different prices, ammount and tax-rates. This is my code:
{
Sum
(
DataBandTax,
(
Positions.UnitPrice *
Positions.Amount *
(
Positions.Article.TaxRate +
100
)
) /
100
)
}
Positions is a Bussines-Object, each object of Positions has exactly one Article.
While Positions.UnitPrice and Positions.Amount are multiplied correctly, Stimulsoft uses for every calculation the same Positions.Article.TaxRate, instead the TaxRate which fits to its Position. For example:
Position | UnitPrice | Amount | TaxRate
1 | 100 | 3 | 5
2 | 50 | 10 | 10
3 | 20 | 5 | 3
So the calculation should be:
((100 * 3 * (5 + 100)) / 100)
+ ((50 * 10 * (10 + 100)) / 100)
+ ((20 * 5 * (3 + 100)) / 100)
= 315 + 550 + 103
= 968
Instead stimulsoft calculates this: (In this example it uses only the TaxRate of Position 1)
((100 * 3) + (50 * 10) + (20 * 5))
* (1+(5/100))
= (300 + 200 + 100) * 1.05
= 600 * 1.05
= 630
How do I stop stimulsoft from doing so?

I was able to solve this issue, by placing the TaxRate inside Positions (instead of Positions.Article). Seems like Stimulsoft has a problem when a sum includes object-values and sub-object-values at the same time.
{
Sum
(
DataBandTax,
(
Positions.UnitPrice *
Positions.Amount *
(
Positions.TaxRate +
100
)
) /
100
)
}

Related

Wilson Score for 1-5 star rating in C#

I found this really well explained calculation but I really don't know if it's correct or not. I've seen a lot of posts on the subject I havent seen one calculate for 5
the most important part is this:
Stars Negative Positive Total
0 0 0 0
1 1 0 1
2 0.75 0.25 1
3 0.5 0.5 1
4 0.25 0.75 1
5 0 1 1
SET new.total = new.positive + new.negative,
new.stars = ROUND( (((new.positive / new.total) * 4) + 1) * 2, 0) / 2,
new.lower_bound = ((new.positive + 1.9208) / (new.positive + new.negative) - 1.96 * SQRT((new.positive * new.negative) / (new.positive + new.negative) + 0.9604) / (new.positive + new.negative)) / (1 + 3.8416 / (new.positive + new.negative))
which I converted to C# as the following
double posf = model.stars == 1 ? 0 :
model.stars == 2 ? 0.25 :
model.stars == 3 ? 0.5 :
model.stars == 4 ? 0.75 :
model.stars == 5 ? 1 : 0;
double negf = model.stars == 1 ? 1 :
model.stars == 2 ? 0.75 :
model.stars == 3 ? 0.5 :
model.stars == 4 ? 0.25 :
model.stars == 5 ? 0 : 0;
rating.positif += posf;
rating.negatif += negf;
rating.ratingcount += 1;
rating.ratingavg = Math.Round((((rating.positif / rating.ratingcount) * 4) + 1) * 2, 0) / 2;
rating.calcSort = ((rating.positif + 1.9208) / (rating.positif + rating.negatif) - 1.96 * Math.Sqrt((rating.positif * rating.negatif) / (rating.positif + rating.negatif) + 0.9604) / (rating.positif + rating.negatif)) / (1 + 3.8416 / (rating.positif + rating.negatif));
Can someone help me if this coding is correct conversion and if the sql version is actually correct.
You can store the lower-bound value as a double, there is no need to round it. The C# function can be turned into something like this:
private static double WilsonAlgorithm(double positive, double negative)
{
return ((positive + 1.9208) / (positive + negative) -
1.96 * Math.Sqrt((positive * negative) / (positive + negative) + 0.9604) /
(positive + negative)) / (1 + 3.8416 / (positive + negative));
}
(credits to: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html, most of it came from the sql code on his page)
To convert from stars --> positive/negative, you can calculate them without over-using the ternary operator for readability.
// constants
const int maxRating = 5;
const int minRating = 1;
const double shareRating = 0.25;
// conversions
var stars = 5;
var positive = (stars - minRating) * shareRating;
var negative = (maxRating - stars) * shareRating;
// ..
// usage?
var lowerBoundRating = WilsonAlgorithm(totalPositive, totalNegative);

Create optional numbers from 3 inserted positive digits

I'm just struggled with question about that:
I need to write a program that get 3 positive digits from the user and print all the 3 digits numbers that can be created from them. I'm not allowed to use recursion.. any ideas?
thanks
Providing that a, b, c are given digits, e.g.
int a = 1;
int b = 2;
int c = 3;
the implementation (C#) could be
String report = String.Join(Environment.NewLine,
new HashSet<int>() {
100 * a + 10 * b + c,
100 * a + 10 * c + b,
100 * b + 10 * a + c,
100 * b + 10 * c + a,
100 * c + 10 * a + b,
100 * c + 10 * b + a,
});
Console.Write(report);
The output is
123
132
213
231
312
321
Note, that for (a = 1, b = 2 and c = 1) you'll get only
121
112
211
I doubt if this solution will be accepted by your professor (even if it doesn't have any recursion), but you can use it as a test when elaborating your own routine.

Get value between a range of two values from percentage

Let's say I have a range of two values:
5...........98
and let's assume the user position's the slider at value 40
Now I want to get the value from another range of values at the exact percentage position as from range 1
let's say the second range of values are 10.........80
int nRange1 = 98 - 5;
int nRange2 = 80 - 10;
int nValue1 = 40;
int nPercentOnRange1 = ((nValue1 - 5) / nRange1)*100;
Now I have to get the value from Range2 at the exact percentage as nPercentOnRange1, but I don't know how
First need to find % from first range and apply that % to new range.
Here is what I will do:
Range1(A to B) Selected value: c
Range2(E to F)
Range1 % = (C-A) / (B-A) * 100
Range 2 corresponding value = ((F - E) * (Range 1 %) / 100) + E
C#:
int Range1Min = 5, Range1Max=90, Range1SelectedValue = 40;
int Range2Min = 6, Range2Max=80;
decimal range1Percent = (Range1SelectedValue-Range1Min ) / (Range1Max-Range1Min) * 100.0
decimal range2NewValue = (Range2Max - Range2Min) * range1Percent / 100 + Range2Min;
Watch out for
int nPercentOnRange1 = ((nValue1 - 5)/ nRange1) * 100;
ending up as zero since nValue1 and nRange1 are integers. This might be better:
int nPercentOnRange1 = ((nValue1 - 5) * 100 / nRange1);
Then you can do
int nValue2 = 10 + nPercentOnRange1*nRange2/100;
The value you need is
x = 10 + nRange2 * nPercentOnRange1 / 100.0
Let me explain why. You need a number x such that
((x - 10) / nRange2) * 100.0 = nPercentOnRange1
Therefore, just solve for x.
((x - 10) / nRange2) * 100.0 = nPercentOnRange1 =>
((x - 10) / nRange2) = nPercentOnRange1 / 100.0 =>
x - 10 = nRange2 * nPercentOnRange1 / 100.0 =>
x = 10 + nRange2 * nPercentOnRange1 / 100.0
And note that this actually makes intuitive sense. We're saying take the percentage, scale that into the length of the second range (that's what nRange2 * nPercentOnRange1 / 100.0) is doing and then add that to the lower bound of the second range. Basically we are saying step nPercentOnRange1 percent into the second range. That's exactly what the formula is expressing.
Perhaps this will work:
nValue2 = nPercentage1 * nRange2 / 100 + 10

Triangle stored as an array. Height and Length of each level?

Assume we have a triangle that each node has K children.
An example for K = 2 is:
1
2 3
4 5 6
An example for K = 3 is:
1
2 3 4
5 6 7 8 9
An example for K = 4 is:
1
2 3 4 5
5 6 7 8 9 1 2
etc.
I would like to store those triangles in an array. I am looking to retrieve the total height of the triangle (assuming they are complete triangles) given the total number of elements T and the number of children per node K
I am also looking to find what is the offset for each element in an array to each children. I know that for the example above where K = 2 the array is [1, 2, 3, 4, 5, 6] where for each level L the offset is L * (L + 1) / 2 (because Level 1 has 1 element, Level 2 has 2, Level 3 has 3 ...)
EDIT: The example is correct. Each node has access to K child nodes. for K = 3 1 has access to 2 3 and 4. 2 has access to 5 6 and 7. 3 has access to 6 7 and 8.
These are triangles and not graphs or trees.
Now that you have clarified your requirement ...
For K=2 there are
1
1+1
1+1+1
...
elements in each level, this is the series 1,2,3,.... If n is the level number then there are n elements at each level. Note that this can also be written as 1+1(n-1)
For K=3 there are
1
1+2
1+2+2
...
elements in each level, this is the series 1,3,5,...; there are 1+2(n-1) elements at each level.
For K=4 there are
1
1+3
1+3+3
...
elements in each level, this is the series 1,4,7,.... There are 1+3(n-1) elements at each level.
At each level in each triangle there are 1+(K-1)(n-1) elements. I expect you can carry on from here.
The total number of elements T for a triangle of height h will be:
T = ∑1...h (1 + (K-1)(n-1))
T = h + (K-1) * ∑1...h (n-1)
T = h + (K-1) * ∑0..h-1 (n)
T = h + (K-1) * ((h-1)² + h-1) / 2
T = h + (K-1) * (h² + 1 - 2h + h-1) / 2
T = h + (K-1) * (h² - h) / 2
Calculating the height
So to get the height h you insert the value of K and solve the equation. Here's an example of an easy case where K equals 3.
T = h + (K-1) * (h² - h) / 2
T = h + (3-1) * (h² - h) / 2
T = h + (h² - h)
T = h²
h = √T
Calculating the offsets
As for the offsets you use the same equation we used to calculate the total number of elements but set h to height-1. Here's an example of getting the offset for row 3 in a triangle with a K of 4.
offset(h) = h-1 + (K-1) * ((h-1)² - (h-1)) / 2
offset(3) = 3-1 + (4-1) * ((3-1)² - (3-1)) / 2
offset(3) = 2 + 3 * (4 - 2) / 2
offset(3) = 5

function to calculate total sale amount

I have a product that sells in a strange way.
The formula to get the current price is:
total_products_sold * 0.05
I need a function that will return the total sales value, given a total_products_sold value.
This needs to figure out all past pricing, using the above formula.
e.g. if I sold 3 products, the total sales amount is:
1 * 0.5 + 2 * 0.5 + 3 * 0.5 = .30
I can't remember the formula, if its a factorial issue or a exponential type equation.
Formula is
total(N) =
1 * 0.5 + 2 * 0.5 + ... + N * 0.5 =
(1 + 2 + ... + N) * 0.5 = (check this link)
((N + 1) * N / 2) * 0.5
function could be something like
float total (int products) {
return (products + 1) * products / 2 * 0.5;
}

Categories