rbwhitaker / CSharpPlayersGuideEarlyAccess

A place to track issues with the C# Player's Guide for patches and future editions
19 stars 0 forks source link

Level 42 - Missing cast on division of two integers #624

Closed dullin closed 2 years ago

dullin commented 2 years ago

Page 337 of Level 42 has a query expression that create a ratio from two ints. Those need to be casted to have a value other than 0.

Code excerpt

var results = from o in objects
              where o.HP > 0
              orderby o.HP
              select o.HP / o.MaxHP;

The same calculation is used on page 339.

rbwhitaker commented 2 years ago

I debated for a while whether to put in the cast or to just make HP and MaxHP be float values. I eventually settled on the second. HP seems like something that would typically not support fractional values, though it isn't impossible. I initially tried the casting solution, but it wasn't just a single place, but about five. And the deciding factor was that they all were in expressions that were already quite complicated, becoming even more complex with the cast. Only the rest of the complexity was to illustrate the features being discussed in the chapter, while the cast was essentially just noise that clouded the key points of the code snippet.