# Double

***

**1. Currency Calculations:**

```csharp
double amount = 123.45;
double taxRate = 0.075;
double taxAmount = amount * taxRate;
double totalAmount = amount + taxAmount;
```

**2. Geometric Calculations:**

```csharp
double radius = 5.0;
double circumference = 2 * Math.PI * radius;
double area = Math.PI * radius * radius;
```

**3. Scientific Calculations:**

```csharp
double x = 2.0;
double y = 3.0;
double z = x * x + y * y;
double distance = Math.Sqrt(z);
```

**4. Unit Conversion:**

```csharp
double inches = 10.0;
double meters = inches * 0.0254;
```

**5. Physics Calculations:**

```csharp
double mass = 10.0; // in kilograms
double acceleration = 9.81; // in meters per second squared
double force = mass * acceleration;
```

**6. Data Analysis:**

```csharp
double[] values = { 1.2, 3.4, 5.6, 7.8, 9.0 };
double sum = values.Sum();
double average = sum / values.Length;
double variance = values.Select(x => (x - average) * (x - average)).Sum() / (values.Length - 1);
```

**7. Financial Modeling:**

```csharp
double principal = 1000.0;
double interestRate = 0.05;
double years = 10.0;
double amount = principal * Math.Pow(1 + interestRate, years);
```

**8. Temperature Conversion:**

```csharp
double fahrenheit = 98.6;
double celsius = (fahrenheit - 32) * 5 / 9;
```

**9. Distance Calculations:**

```csharp
double x1 = 1.0;
double y1 = 2.0;
double x2 = 3.0;
double y2 = 4.0;
double distance = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
```

**10. Random Number Generation:**

```csharp
Random random = new Random();
double randomNumber = random.NextDouble();
```

**11. Probability Calculations:**

```csharp
double probability = 0.5;
bool outcome = (random.NextDouble() < probability);
```

**12. Trigonometric Calculations:**

```csharp
double angle = Math.PI / 3;
double sine = Math.Sin(angle);
double cosine = Math.Cos(angle);
double tangent = Math.Tan(angle);
```

**13. Interpolation:**

```csharp
double[] xValues = { 1.0, 2.0, 3.0 };
double[] yValues = { 2.0, 4.0, 6.0 };
double x = 1.5;
double y = (yValues[1] - yValues[0]) / (xValues[1] - xValues[0]) * (x - xValues[0]) + yValues[0];
```

**14. Curve Fitting:**

```csharp
double[] xValues = { 1.0, 2.0, 3.0 };
double[] yValues = { 2.0, 4.0, 6.0 };
double[] coefficients = new double[2];
coefficients[0] = yValues[0];
coefficients[1] = (yValues[1] - yValues[0]) / (xValues[1] - xValues[0]);
double y = coefficients[0] + coefficients[1] * x;
```

**15. Numerical Integration:**

```csharp
double[] xValues = { 1.0, 2.0, 3.0 };
double[] yValues = { 2.0, 4.0, 6.0 };
double integral = 0.0;
for (int i = 0; i < xValues.Length - 1; i++)
{
    integral += (yValues[i] + yValues[i + 1]) * (xValues[i + 1] - xValues[i]) / 2;
}
```

**16. Vector Operations:**

```csharp
double[] vector1 = { 1.0, 2.0, 3.0 };
double[] vector2 = { 4.0, 5.0, 6.0 };
double dotProduct = vector1[0] * vector2[0] + vector1[1] * vector2[1] + vector1[2] * vector2[2];
```

**17. Matrix Operations:**

```csharp
double[,] matrix1 = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double[,] matrix2 = { { 5.0, 6.0 }, { 7.0, 8.0 } };
double[,] result = new double[2, 2];
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 2; j++)
    {
        result[i, j] = matrix1[i, 0] * matrix2[0, j] + matrix1[i, 1] * matrix2[1, j];
    }
}
```

**18. Complex Number Operations:**

```csharp
double real1 = 1.0;
double imaginary1 = 2.0;
double real2 = 3.0;
double imaginary2 = 4.0;
double complex1 = real1 + imaginary1 * Complex.ImaginaryOne;
double complex2 = real2 + imaginary2 * Complex.ImaginaryOne;
double complexSum = complex1 + complex2;
```

**19. Data Validation:**

```csharp
double value = 123.45;
if (Double.IsNaN(value))
{
    throw new ArgumentException("Value is NaN.");
}
else if (Double.IsInfinity(value))
{
    throw new ArgumentException("Value is infinity.");
}
```

**20. Rounding:**

```csharp
double value = 123.456789;
double rounded = Math.Round(value, 2); // Rounds to the nearest hundredth
```

**21. Formatting:**

```csharp
double value = 1234567.89;
string formattedValue = value.ToString("N2"); // Formats as "1,234,567.89"
```

**22. Parsing:**

```csharp
string input = "123.45";
double parsedValue = Double.Parse(input);
```

**23. Comparison:**

```csharp
double value1 = 1.0;
double value2 = 1.0000000001;
if (Double.Equals(value1, value2))
{
    // The values are equal within a tolerance of 15 decimal places.
}
```

**24. Maximum and Minimum:**

```csharp
double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0 };
double maxValue = values.Max();
double minValue = values.Min();
```

**25. Clamping:**

```csharp
double value = -123.45;
double clampedValue = Math.Clamp(value, -100.0, 100.0); // Clamps the value to the range [-100.0, 100.0]
```

**26. Bitwise Operations:**

```csharp
double value = 123.45;
double binaryValue = BitConverter.DoubleToInt64Bits(value); // Converts the value to its binary representation
```

**27. Floating-Point Arithmetic:**

```csharp
double value1 = 0.1;
double value2 = 0.2;
double sum = value1 + value2; // Not exactly 0.3 due to floating-point arithmetic limitations
```

**28. Overflow and Underflow:**

```csharp
double value = Double.MaxValue;
double overflowValue = value * 2.0; // Overflow occurs
double underflowValue = value / 2.0; // Underflow occurs
```

**29. NaN and Infinity:**

```csharp
double NaNValue = Double.NaN; // Represents a Not-a-Number value
double infinityValue = Double.PositiveInfinity; // Represents a positive infinity value
```

**30. Precision and Accuracy:**

```csharp
double value = 123.456789;
double roundedValue = Math.Round(value, 2); // Loses precision due to rounding
```

**31. Cross-Platform Compatibility:**

```csharp
double value = 123.45;
byte[] bytes = BitConverter.GetBytes(value); // Converts the value to a byte array for cross-platform compatibility
```

**32. String Interpolation:**

```csharp
double value = 123.45;
string message = $"The value is {value}";
```

**33. LINQ Queries:**

```csharp
double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0 };
var evenValues = values.Where(x => x % 2 == 0); // Filters even values
```

**34. Lambda Expressions:**

```csharp
double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0 };
double sum = values.Sum(x => x * x); // Calculates the sum of the squares of the values
```

**35. Anonymous Types:**

```csharp
double x = 1.0;
double y = 2.0;
var point = new { X = x, Y = y }; // Creates an anonymous type representing a point
```

**36. Extension Methods:**

```csharp
public static double Square(this double value)
{
    return value * value;
}
double value = 2.0;
double squaredValue = value.Square(); // Calls the extension method to square the value
```

**37. Nullable Types:**

```csharp
double? nullableValue = null;
if (nullableValue.HasValue)
{
    double value = nullableValue.Value; // Unwraps the nullable value
}
```

**38. Parallel Programming:**

```csharp
double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0 };
Parallel.ForEach(values, value =>
{
    double squaredValue = value * value; // Squares each value in parallel
});
```

**39. Asynchronous Programming:**

```csharp
async Task<double> GetValueAsync()
{
    // Asynchronously retrieves a double value from a remote service
    await Task.Delay(1000);
    return 123.45;
}
```

**40. Event Handling:**

```csharp
public class ValueChangedEventArgs : EventArgs
{
    public double NewValue { get; set; }
}

public class ValueChangedHandler
{
    public event EventHandler<ValueChangedEventArgs> ValueChanged;

    public double Value { get; set; }

    public void OnValueChanged()
    {
        ValueChanged?.Invoke(this, new ValueChangedEventArgs { NewValue = Value });
    }
}
```

**41. Delegates:**

```csharp
public delegate double DoubleOperation(double value);

public static double Square(double value)
{
    return value * value;
}

DoubleOperation operation = Square;
double value = 2.0;
double squaredValue = operation(value); // Calls the delegate to square the value
```

**42. Generics:**

```csharp
public class GenericContainer<T>
{
    private T _value;

    public GenericContainer(T value)
    {
        _value = value;
    }

    public T GetValue()
    {
        return _value;
    }
}

double value = 123.45;
GenericContainer<double> container = new GenericContainer<double>(value);
double retrievedValue = container.GetValue();
```

**43. Reflection:**

```csharp
Type type = typeof(double);
PropertyInfo property = type.GetProperty("MaxValue");
double maxValue = (double)property.GetValue(null); // Gets the maximum value of a double
```

**44. Attributes:**

```csharp
[AttributeUsage(AttributeTargets.Property)]
public class RangeAttribute : Attribute
{
    public double Min { get; set; }
    public double Max { get; set; }

    public RangeAttribute(double min, double max)
    {
        Min = min;
        Max = max;
    }
}

[Range(-100.0, 100.0)]
public double Value { get; set; }
```

**45. Custom Exceptions:**

```csharp
public class InvalidValueException : Exception
{
    public InvalidValueException(double value) : base($"Invalid value: {value}")
    {
    }
}

public static void ValidateValue(double value)
{
    if (value < 0.0)
    {
        throw new InvalidValueException(value);
    }
}
```

**46. Unit Testing:**

```csharp
[Fact]
public void GetMaxValue_ReturnsMaxValue()
{
    double maxValue = Double.MaxValue;
    Assert.Equal(maxValue, GetMaxValue());
}
```

**47. Mocking:**

```csharp
public interface IDoubleService
{
    double GetMaxValue();
}

public class DoubleServiceMock : IDoubleService
{
    public double GetMaxValue()
    {
        return 9999.99; // Mocked value instead of the actual maximum value
    }
}

public class DoubleProcessor
{
    private readonly IDoubleService _doubleService;

    public DoubleProcessor(IDoubleService doubleService)
    {
        _doubleService = doubleService;
    }

    public double GetMaxValue()
    {
        return _doubleService.GetMaxValue();
    }
}
```

**48. Code Contracts:**

```csharp
[ContractInvariantMethod]
private void ObjectInvariant()
{
    Contract.Invariant(Value >= 0.0);
}
```

**49. Code Analysis:**

```csharp
// Suppress the "Use round-trippable type double for message contracts" rule
[SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
private double _value;
```

**50. Code Generation:**

```csharp
public static class DoubleExtensions
{
    public static string ToCurrencyString(this double value)
    {
        return value.ToString("C2");
    }
}
```
