I am trying to solve this question :
Create a function of type Generic so that it receives 5 marks, regardless of the type of these marks. Calculate the average, calculate the marks above and below the average, and print the result on the screen.
but I did not know how! , I do this but I don't know how to display the marks above and below the average.
and I am not sure if I solve the question in the right way.
using System;
namespace oopGeneric
{
class Calculator
{
public int high = 0, low = 0;
public double Sum<A, B, C, D, E>(int n1, double n2, double n3, int n4, int n5)
{
double sum = n1 + n2 + n3 + n4 + n5;
Console.WriteLine("The Summation {0} ", sum);
double avg = sum / 5;
return avg;
}
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
double sum = calc.Sum<int, double, double, int, int>(5, 5.5, 15.0, 6, 8);
// Console.WriteLine("above" + high);
Console.WriteLine(sum);
}
}
}
}
In the method Sum, you can insert the code
double avg = sum / 5;
var marks = new double[]{n1,n2,n3,n4,n5};
foreach (double m in marks)
if (m > avg) Console.WriteLine(m);
You will get the marks above the average.
This is the solution using matching pattern.
public double Sum<A, B, C, D, E>(A n1, B n2, C n3, D n4, E n5)
{
double sum = 0;
sum+= (n1 is Int32 n1_as_integer) ? n1_as_integer :
(n1 is Double n1_as_double) ? n1_as_double : 0;
sum+= (n2 is Int32 n2_as_integer) ? n2_as_integer :
(n2 is Double n2_as_double) ? n2_as_double : 0;
return sum / 5;
}
This is the solution using params + matching pattern.
public double Sum(params object[] N)
{
double sum = 0;
foreach(var i in N)
sum+= (i is Int32 i_as_integer) ? i_as_integer :
(i is Double i_as_double) ? i_as_double : 0;
return (N.Lenght>0) ? sum / N.Lenght : 0;
}
You may want to consider accepting any IConvertible object as a generic constraint.
IConvertible
An object that implements IConvertible is required to implement public methods that either convert to the designated primitive runtime type(such as int, float) or throw an InvalidCastException.
These types include
bool sbyte byte
u/short u/int u/long
double float decimal Single
DateTime char string
We can use IConvertible as a constraint to allow only objects that can be converted to primitives.
For example we could convert any of the above types(except the ones that aren't numbers they will throw InvalidCastException) to a double with this method.
double Convert<T>(T Value) where T : IConvertible
{
return Value.ToDouble(null);
}
// These all work
Convert(10m); // (decimal)
Convert(10); // (int)
Convert(10f); // (float)
Convert((sbyte)10);
Convert((long)10);
Using this we can construct an average function that can accept an atrocity such as this:
Average<IConvertible>(10, 10m, (ushort)10, (byte)10, (sbyte)10, 10f, 10d, (long)10, (ulong)10, (uint)10);
Which if we did everything right should ideally output 10d;
public double Average<T>(params T[] numbers) where T : IConvertible
{
// create a place to put the sum, since we need it to calculate the average later
double sum = default;
// store the length of the array since we use it in multiple places
int count = numbers.Length;
for (int i = 0; i < count; i++)
{
// convert value to double, all primitive IConvertible types are required to cast to double(generally)
double value = numbers[i].ToDouble(null);
// add value to the sum so we can calculate the average later
sum += value;
}
// return the average score
return sum / (double)count; // cast int to double to avoid unintentional integer math
}
Editors Note
The above example assumes the requirement to use Generics. You can simplify the un-intuitive call for Average<IConvertible> for mixed-types with a solution such as Average(params IConverible[] numbers), however that would not be using generics.
I am looking for the fastest way in C# to round a value to the nearest power of two.
I've discovered that the fastest way to round a value to the next power of two if to use bitwise operators like this.
int ToNextNearest(int x)
{
if (x < 0) { return 0; }
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
But this gives the next nearest and not the nearest and I would like to only have the nearest power of two.
Here is a simple way to do that.
int ToNearest(int x)
{
Math.Pow(2, Math.Round(Math.Log(x) / Math.Log(2)));
}
But is there a better optimized version of finding the nearest power of two value ?
Thanks a lot.
Surely the best way is to use your bitwise routine to find the next power of two, then divide that result by two. This gives you the previous power of two. Then a simple comparison will tell you which of the two is closer.
int ToNearest(int x)
{
int next = ToNextNearest(x);
int prev = next >> 1;
return next - x < x - prev ? next : prev;
}
Untested code but you get the idea.
I'm using this:
public static int CeilPower2(int x)
{
if (x < 2) {
return 1;
}
return (int) Math.Pow(2, (int) Math.Log(x-1, 2) + 1);
}
public static int FloorPower2(int x)
{
if (x < 1) {
return 1;
}
return (int) Math.Pow(2, (int) Math.Log(x, 2));
}
On .Net Core, the fastest way to do this would probably to use the intrinsics operations:
private static int NearestPowerOf2(uint x)
{
return 1 << (sizeof(uint) * 8 - BitOperations.LeadingZeroCount(x - 1));
}
On CPU supporting the LZCNT instructions, it is just 6 CPU instructions, without branching.
.Net6 has introduced a method for this
using System.Numerics;
var nearestPowOf2 = BitOperations.RoundUpToPowerOf2(100); //returns 128
How about this:
int ToNearest(int val, int pow)
{
if (pow < 0) return 0;
if (pow == 0) return val;
if (val & (1 << (pow - 1))) {
return ((val >> pow) + 1) << pow;
} else {
return (val >> pow) << pow;
}
}
Haven't tested but i think this could work
int ToNearest(value x)
{
int num = 0;
for(int i=1; i < 65; i++)
{
int cur = Math.Abs(value - 0<<i);
if(Math.Abs(value - 0<<i) < Math.Abs(value - num))
num = cur;
else if(num != 0) break;
}
return num;
}
This is the full implementation of the suggested solution of #john, with the change that it will round up if the value is exactly in between the next and previous power of two.
public static int RoundToNextPowerOfTwo(int a)
{
int next = CeilToNextPowerOfTwo(a);
int prev = next >> 1;
return next - a <= a - prev ? next : prev;
}
public static int CeilToNextPowerOfTwo(int number)
{
int a = number;
int powOfTwo = 1;
while (a > 1)
{
a = a >> 1;
powOfTwo = powOfTwo << 1;
}
if (powOfTwo != number)
{
powOfTwo = powOfTwo << 1;
}
return powOfTwo;
}
Since C# requires IEEE754 floats there is probably a faster way on any platform that does not emulate the floating point functions:
int ToNearestPowerOf2(int x) =>
1 << (int)(BitConverter.DoubleToInt64Bits(x + x/3) >> 52) - 1023;
Rationale:
x + x/3
nearest power of 2, basically *4/3
(BitConverter.DoubleToInt64Bits(x) >> 52) - 1023
take floating point exponent for floor(ln2(x))
1 << x
exponential function with base 2
The function obviously requires a positive value for x.
0 won't work because the closest power of 2 is -∞,
and negative values have a complex logarithms.
Whether this is the fastest way will probably highly depend on what the JIT optimizer squeezes out of the code, more specifically how it handles the hard pointer cast in DoubleToInt64Bits. This may prevent other optimizations.
You do not have to use any comparison to get the nearest power of 2. Since all powers of two are separated by the same factor the rounding point is always at 3/4 of the next power of 2 (i.e. exactly the topmost 2 bits are set). So multiplication by the reciprocal followed by truncation will do the job.
How to simplify a fraction in C#? For example, given 1 11/6, I need it simplified to 2 5/6.
If all you want is to turn your fraction into a mixed number whose fractional part is a proper fraction like the previous answers assumed, you only need to add numerator / denominator to the whole part of the number and set the numerator to numerator % denominator. Using loops for this is completely unnecessary.
However the term "simplify" usually refers to reducing a fraction to its lowest terms. Your example does not make clear whether you want that as well, as the example is in its lowest terms either way.
Here's a C# class that normalizes a mixed number, such that each number has exactly one representation: The fractional part is always proper and always in its lowest terms, the denominator is always positive and the sign of the whole part is always the same as the sign of the numerator.
using System;
public class MixedNumber {
public MixedNumber(int wholePart, int num, int denom)
{
WholePart = wholePart;
Numerator = num;
Denominator = denom;
Normalize();
}
public int WholePart { get; private set; }
public int Numerator { get; private set; }
public int Denominator { get; private set; }
private int GCD(int a, int b)
{
while(b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
private void Reduce(int x) {
Numerator /= x;
Denominator /= x;
}
private void Normalize() {
// Add the whole part to the fraction so that we don't have to check its sign later
Numerator += WholePart * Denominator;
// Reduce the fraction to be in lowest terms
Reduce(GCD(Numerator, Denominator));
// Make it so that the denominator is always positive
Reduce(Math.Sign(Denominator));
// Turn num/denom into a proper fraction and add to wholePart appropriately
WholePart = Numerator / Denominator;
Numerator %= Denominator;
}
override public String ToString() {
return String.Format("{0} {1}/{2}", WholePart, Numerator, Denominator);
}
}
Sample usage:
csharp> new MixedNumber(1,11,6);
2 5/6
csharp> new MixedNumber(1,10,6);
2 2/3
csharp> new MixedNumber(-2,10,6);
0 -1/3
csharp> new MixedNumber(-1,-10,6);
-2 -2/3
int unit = 1;
int numerator = 11;
int denominator = 6;
while(numerator >= denominator)
{
numerator -= denominator;
if(unit < 0)
unit--;
else
unit++;
}
Then do whatever you like with the variables.
Note that this isn't particularly general.... in particular I doubt it's going to play well with negative numbers (edit: might be better now)
int num = 11;
int denom = 6;
int unit = 1;
while (num >= denom)
{
num -= denom;
unit++;
}
Sorry I didn't fully understand that part about keeping track of the unit values.
to simplify the fraction 6/11 you would see if you could get both to line up by the same number greater than 1 and divide.
So
2,4,6,8,10,12 no
1,3,6,9,12 no
4,8 no
5,10 no
6,12 no
7 no
8 no
9 no.
No will be the answer for all so it is already in simplest. There is no more to be done.
I know this sounds like a homework assignment, but it isn't. Lately I've been interested in algorithms used to perform certain mathematical operations, such as sine, square root, etc. At the moment, I'm trying to write the Babylonian method of computing square roots in C#.
So far, I have this:
public static double SquareRoot(double x) {
if (x == 0) return 0;
double r = x / 2; // this is inefficient, but I can't find a better way
// to get a close estimate for the starting value of r
double last = 0;
int maxIters = 100;
for (int i = 0; i < maxIters; i++) {
r = (r + x / r) / 2;
if (r == last)
break;
last = r;
}
return r;
}
It works just fine and produces the exact same answer as the .NET Framework's Math.Sqrt() method every time. As you can probably guess, though, it's slower than the native method (by around 800 ticks). I know this particular method will never be faster than the native method, but I'm just wondering if there are any optimizations I can make.
The only optimization I saw immediately was the fact that the calculation would run 100 times, even after the answer had already been determined (at which point, r would always be the same value). So, I added a quick check to see if the newly calculated value is the same as the previously calculated value and break out of the loop. Unfortunately, it didn't make much of a difference in speed, but just seemed like the right thing to do.
And before you say "Why not just use Math.Sqrt() instead?"... I'm doing this as a learning exercise and do not intend to actually use this method in any production code.
First, instead of checking for equality (r == last), you should be checking for convergence, wherein r is close to last, where close is defined by an arbitrary epsilon:
eps = 1e-10 // pick any small number
if (Math.Abs(r-last) < eps) break;
As the wikipedia article you linked to mentions - you don't efficiently calculate square roots with Newton's method - instead, you use logarithms.
float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;}
This is my favorite fast square root. Actually it's the inverse of the square root, but you can invert it after if you want....I can't say if it's faster if you want the square root and not the inverse square root, but it's freaken cool just the same.
http://www.beyond3d.com/content/articles/8/
What you are doing here is you execute Newton's method of finding a root. So you could just use some more efficient root-finding algorithm. You can start searching for it here.
Replacing the division by 2 with a bit shift is unlikely to make that big a difference; given that the division is by a constant I'd hope the compiler is smart enough to do that for you, but you may as well try it to see.
You're much more likely to get an improvement by exiting from the loop early, so either store new r in a variable and compare with old r, or store x/r in a variable and compare that against r before doing the addition and division.
Instead of breaking the loop and then returning r, you could just return r. May not provide any noticable increase in performance.
With your method, each iteration doubles the number of correct bits.
Using a table to obtain the initial 4 bits (for example), you will have 8 bits after the 1st iteration, then 16 bits after the second, and all the bits you need after the fourth iteration (since a double stores 52+1 bits of mantissa).
For a table lookup, you can extract the mantissa in [0.5,1[ and exponent from the input (using a function like frexp), then normalize the mantissa in [64,256[ using multiplication by a suitable power of 2.
mantissa *= 2^K
exponent -= K
After this, your input number is still mantissa*2^exponent. K must be 7 or 8, to obtain an even exponent. You can obtain the initial value for the iterations from a table containing all the square roots of the integral part of mantissa. Perform 4 iterations to get the square root r of mantissa. The result is r*2^(exponent/2), constructed using a function like ldexp.
EDIT. I put some C++ code below to illustrate this. The OP's function sr1 with improved test takes 2.78s to compute 2^24 square roots; my function sr2 takes 1.42s, and the hardware sqrt takes 0.12s.
#include <math.h>
#include <stdio.h>
double sr1(double x)
{
double last = 0;
double r = x * 0.5;
int maxIters = 100;
for (int i = 0; i < maxIters; i++) {
r = (r + x / r) / 2;
if ( fabs(r - last) < 1.0e-10 )
break;
last = r;
}
return r;
}
double sr2(double x)
{
// Square roots of values in 0..256 (rounded to nearest integer)
static const int ROOTS256[] = {
0,1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,
12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16 };
// Normalize input
int exponent;
double mantissa = frexp(x,&exponent); // MANTISSA in [0.5,1[ unless X is 0
if (mantissa == 0) return 0; // X is 0
if (exponent & 1) { mantissa *= 128; exponent -= 7; } // odd exponent
else { mantissa *= 256; exponent -= 8; } // even exponent
// Here MANTISSA is in [64,256[
// Initial value on 4 bits
double root = ROOTS256[(int)floor(mantissa)];
// Iterate
for (int it=0;it<4;it++)
{
root = 0.5 * (root + mantissa / root);
}
// Restore exponent in result
return ldexp(root,exponent>>1);
}
int main()
{
// Used to generate the table
// for (int i=0;i<=256;i++) printf(",%.0f",sqrt(i));
double s = 0;
int mx = 1<<24;
// for (int i=0;i<mx;i++) s += sqrt(i); // 0.120s
// for (int i=0;i<mx;i++) s += sr1(i); // 2.780s
for (int i=0;i<mx;i++) s += sr2(i); // 1.420s
}
Define a tolerance and return early when subsequent iterations fall within that tolerance.
Since you said the code below was not fast enough, try this:
static double guess(double n)
{
return Math.Pow(10, Math.Log10(n) / 2);
}
It should be very accurate and hopefully fast.
Here is code for the initial estimate described here. It appears to be pretty good. Use this code, and then you should also iterate until the values converge within an epsilon of difference.
public static double digits(double x)
{
double n = Math.Floor(x);
double d;
if (d >= 1.0)
{
for (d = 1; n >= 1.0; ++d)
{
n = n / 10;
}
}
else
{
for (d = 1; n < 1.0; ++d)
{
n = n * 10;
}
}
return d;
}
public static double guess(double x)
{
double output;
double d = Program.digits(x);
if (d % 2 == 0)
{
output = 6*Math.Pow(10, (d - 2) / 2);
}
else
{
output = 2*Math.Pow(10, (d - 1) / 2);
}
return output;
}
I have been looking at this as well for learning purposes. You may be interested in two modifications I tried.
The first was to use a first order taylor series approximation in x0:
Func<double, double> fNewton = (b) =>
{
// Use first order taylor expansion for initial guess
// http://www27.wolframalpha.com/input/?i=series+expansion+x^.5
double x0 = 1 + (b - 1) / 2;
double xn = x0;
do
{
x0 = xn;
xn = (x0 + b / x0) / 2;
} while (Math.Abs(xn - x0) > Double.Epsilon);
return xn;
};
The second was to try a third order (more expensive), iterate
Func<double, double> fNewtonThird = (b) =>
{
double x0 = b/2;
double xn = x0;
do
{
x0 = xn;
xn = (x0*(x0*x0+3*b))/(3*x0*x0+b);
} while (Math.Abs(xn - x0) > Double.Epsilon);
return xn;
};
I created a helper method to time the functions
public static class Helper
{
public static long Time(
this Func<double, double> f,
double testValue)
{
int imax = 120000;
double avg = 0.0;
Stopwatch st = new Stopwatch();
for (int i = 0; i < imax; i++)
{
// note the timing is strictly on the function
st.Start();
var t = f(testValue);
st.Stop();
avg = (avg * i + t) / (i + 1);
}
Console.WriteLine("Average Val: {0}",avg);
return st.ElapsedTicks/imax;
}
}
The original method was faster, but again, might be interesting :)
Replacing "/ 2" by "* 0.5" makes this ~1.5 times faster on my machine, but of course not nearly as fast as the native implementation.
Well, the native Sqrt() function probably isn't implemented in C#, it'll most likely be done in a low-level language, and it'll certainly be using a more efficient algorithm. So trying to match its speed is probably futile.
However, in regard to just trying to optimize your function for the heckuvit, the Wikipedia page you linked recommends the "starting guess" to be 2^floor(D/2), where D represents the number of binary digits in the number. You could give that an attempt, I don't see much else that could be optimized significantly in your code.
You can try
r = x >> 1;
instead of / 2 (also in the other place you device by 2).
It might give you a slight edge.
I would also move the 100 into the loop. Probably nothing, but we are talking about ticks in here.
just checking it now.
EDIT:
Fixed the > into >>, but it doesn't work for doubles, so nevermind.
the inlining of the 100 gave me no speed increase.