Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Hash function for arrays of floats and ints in .NET

I have a particular use case where I am wanting to hash several arrays of float and int together. I am writing a simulation where I want to memoize a function call. The function takes a Settings type as an input.

The values in the Capacities and MaxRates arrays are 64 bit floats restricted to a range of 0.0 to 1,000,000.0. The length of Capacities and MaxRates is also restricted to 0 to 100. ValveState is an int with values from 0 to 10.

I am aware that hashing arbitrary floats is a bad idea. In this particular use case though the values in Settings will be exact, bit for bit matches to previous combination of values. That is the reason I want to memoize a function call using a Dictionary<Settings, 'Result>.

The function call I want to memoize is expensive and takes up >90% of the runtime. The combination of values in Settings is frequently repeated and they are exact matches. No math is actually done with the values during the simulation so rounding error is not occurring. Early tests suggest that memoizing this function can lead to a 10x improvement in speed with no loss in accuracy.

I am working in F# but anything in .NET is acceptable since I can translate it. Here is an example of what the F# record looks like.

type Settings = {
    Capacities : array<float>
    MaxRates : array<float>
    ValveState : array<int>
} with
    override this.GetHashCode () = 
        // Your awesome suggestion here
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use HashCode.Combine (available in netstandard 2.1) to combine hashes of all values in all three arrays.

open System

let hashArray (arr : 'T array) =
    let mutable hash = 0
    for x in arr do
        hash <- HashCode.Combine(hash, x)
    hash

[<CustomEquality; NoComparison>]
type Settings = {
    Capacities : array<float>
    MaxRates : array<float>
    ValveState : array<int>
} with
    override this.GetHashCode () =
        HashCode.Combine(
            hashArray this.Capacities,
            hashArray this.MaxRates,
            hashArray this.ValveState
        )

    override this.Equals(o : obj) =
        match o with
        | :? Settings as s ->
            this.Capacities = s.Capacities &&
            this.MaxRates = s.MaxRates &&
            this.ValveState = s.ValveState
        | _ ->
            false
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!