- #1
SlurrerOfSpeech
- 141
- 11
Was doing this HackerRank problem and my solution is
but I'm wondering whether I should be using float instead of double. Which is more precise in this case?
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
Console.ReadLine(); // skip first line, don't need it in C# implementation
int[] nums = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
// helper function
Func<int[], Func<int,bool>, double> ProportionSatisfyingCondition =
(arr, unpred) => (double)arr.Where(unpred).Count() / (double)arr.Length;
// compute and print proportions
Console.WriteLine(ProportionSatisfyingCondition(nums, i => i > 0));
Console.WriteLine(ProportionSatisfyingCondition(nums, i => i < 0));
Console.WriteLine(ProportionSatisfyingCondition(nums, i => i == 0));
}
}
but I'm wondering whether I should be using float instead of double. Which is more precise in this case?