4

Me talking to my prof about the app that i have to make to pass the oop course early.

Prof> So can you add some kind of a random algorithm to that? You can use float for tha....
Me> NOOOOOOOO
Prof> ...
Me> ...
Me> I wont use floats, i will recreate fractions if i have to.
Prof> Why? There must be a way to compare floats easly...
Me> Nope. If you hate somebody, make them do somthing using floats. I will do that my way.
Prof> okay...

I nearly got myself in bigger shit that im now. I still have to make the app in C++ (Big OOF) but at least i wont have to dick around float arythmetric.
I wish i could do it in C#... I dont like that memory managment...

Comments
  • 3
    If somebody thinks that floats are easy...
    Then yes they are easy like 0.1+0.1+0.5+0.3 == 1 right?
  • 5
    Floats are not that difficult. You just don't compare for equality, EVER. Instead, you use <, >, or test for an interval.

    On the other hand, you can often get away with integer arithmetics in proper resolution, like basing all math on tenths or hundreds of a cent in currency arithmetics.
  • 2
    I have a floatEq function that I use everywhere
    abs(a-b} < 10 * FLOAT_MIN
    Since float_min is a negligible value in most situations, for the rest you reimplement it with defined behavior anyway.
  • 0
    @Lor-inc careful. The error spreads and it could fail if before using that you perform several operations that introduce new error.
  • 0
    @Gregozor2121 sadly floating point arithmetics is part of your job. Learn to deal with it, you won't always be able to work around it. It's not trivial but it's not one of the most difficult topics anyway.
  • 0
    @Pickman If I encounter problems like that, I increase the tolerance. Rounding errors of any kind follow normal distribution so at 40*FLOAT_MIN it's safe to assume that the error will never cross it.
  • 0
    @Lor-inc Complex numerics can be pretty difficult to get right because you also have to look at your problem domain, e.g. badly conditioned things. But once you get into that rabbit hole, you'd use existing numerics software anyway instead of writing it yourself.
  • 1
    You may not like memory management but you definitely should learn learn it and embrace it these people who run around using JRE, and .NET and the just “code to a SDK” because pointers, allocating and deallocating “is scary” ...nooooo once you truly understand it you’ll understand the power and never use a garbage collector again. I use to be java and C# developer then I learned C, assembler and C++ never will I return to the “utopia lie” of run time environments
  • 0
    @Fast-Nop and don‘t check modulo results either
  • 0
    @Lor-inc you should really try a different approach. Numeric instability is a thing and can lead to rather severe changes, I'm speaking the order of 10e01 after just a few bad operations. In short there's no catch-all solution but as long as you know what is happening you will be fine. Essentially don't put that approach in a library XD
Add Comment