# Float64, a Hash, and Nil

**URL:** <https://forum.crystal-lang.org/t/float64-a-hash-and-nil/1473>\
**Category:** Help & Support\
**Created:** [December 11, 2019, 7:30am UTC](https://forum.crystal-lang.org/t/float64-a-hash-and-nil/1473 "2019-12-11T07:30:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darenw](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/darenw/32/647_2.png) [@darenw](https://forum.crystal-lang.org/u/darenw)\
**Post date:** [December 11, 2019, 7:30am UTC](https://forum.crystal-lang.org/t/float64-a-hash-and-nil/1473/1 "2019-12-11T07:30:12Z")

</div>

How to get out of this bit of trouble? I have:

```
best_so_far = { "dems" => nil, "solver" => nil, "cost" => 9999999.9 } 

```

and later on I have, where cost is a Float64:

```
if cost < best_so_far["cost"]
    ...whatever...
end

```

The ‘if’ statement is tripping up the compiler. It complains  
Error: no overload matches ‘Float64#\<’ with type (Float64 | Nil)

---

<div class="post-metadata">

**Author:** ![Exilor](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/exilor/32/780_2.png) [@Exilor](https://forum.crystal-lang.org/u/Exilor)\
**Post date:** [December 11, 2019, 8:21am UTC](https://forum.crystal-lang.org/t/float64-a-hash-and-nil/1473/2 "2019-12-11T08:21:22Z")

</div>

You can do:

```crystal
if (temp = best_so_far["cost"]) && cost < temp
end

```

or:

```crystal
if cost < best_so_far["cost"].not_nil!
end

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [December 11, 2019, 8:21am UTC](https://forum.crystal-lang.org/t/float64-a-hash-and-nil/1473/3 "2019-12-11T08:21:36Z")

</div>

If you know the “cost” key is not nil you can do `if cost < best_so_far["cost"].not_nil!`.

Depends on what you are trying to do, Hash might not be the best type to use here. Maybe a class with explicitly named properties might be better, something like:

```crystal
class BestSoFar
  property dems : NoIdeaWhatTypeYouWant? = nil
  property solver : NoIdeaWhatTypeYouWant? = nil
  property cost = 9999999.9
end

```

---

<div class="post-metadata">

**Author:** ![darenw](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/darenw/32/647_2.png) [@darenw](https://forum.crystal-lang.org/u/darenw)\
**Post date:** [December 11, 2019, 8:38am UTC](https://forum.crystal-lang.org/t/float64-a-hash-and-nil/1473/4 "2019-12-11T08:38:04Z")

</div>

The class BestSoFar looks much better! That is how I’m doing it now. BTW, it’s a combinatorial optimizer I’m working on.

What the heck was I thinking? Why didn’t I think of that in the first place? Probably was doing too much Javascript the last few days.
