Sometimes these… things, just happen. Bizarre things. Paranormal things. Things I have no explanation for. Here’s one. So I was refactoring some code and I had this:
return (int)floor(_speed / 100.0);
Well, don’t known if it is really the cause, but that _speed variable has a “cursed” name:
I assume the void return type wasn’t in your actual code.
Wouldn’t the second print out of _speed never get executed given that it is after the return?
@serge: Well the rule seems to be “begins with an underscore followed by an uppercase letter”, and mine are lowercase. Although I also use double-underscore sometimes… I might revise it later just in case.
@sir_nacnud: Whoops you’re right, fixed.
And yeah the second print probably wouldn’t occur, but I was desperate! I also put prints around every call to the function and still no odd values. Spooky!
This may be my lack of experience speaking, but if this is an integer division, why do you have a decimal point in there?
@AMX: It’s just good practice to ensure the result comes out how I want. Say if I was doing 2050 / 100 (20.5 in decimal), depending on the language it might come out as 20 or 21. This way I’m sure it’ll always be rounded down to 20.
Also if I was doing a longer operation, I keep the decimal precision throughout and only round it in the end, reducing rounding errors.
Hi, i believe your weird “-XXXXXXXXX” result is probably given by 2 factors:
1) The negative number is not really that, it’s just the result that is too big to be presented in that output and it literally shows “anything”.
2) The resulting number cannot be stored in an integer data type, so what is actually stored is the memory address of the result…
Anyway, try to use a proper data type to store the result and do not parse it, save it to a double type container and output it to a proper a renderer that shouldn’t parse the result either (text output probably). Make sure you will not force cast an int type too ( (int)floor… ).
Is _spees a global variable? If not it maybe your function cant acces to it so it returns some odd number as if variable _speed isnt initialized jet.
Also why don’t you pas _speed as input parameter to a funcion?
I’ve found that sometimes this sort of error has nothing to do with the variable itself, but rather how C++ is resolving the object. This looks like an inherited function, so has it been delared virtual? Have you inadvertently used a static cast to the wrong subtype? Has the pointer to your craft object changed somehow since the last time you used it?
C++ is a bit dangerous like that, and these errors can be really hard to track down!
Did you remember to #include ? I don’t know about C++, but if you fail to give C a prototype for a function, C will “helpfully” guess that it returns int and accepts any number of arguments. If the function actually returns a float, you’re likely to get a huge negative number. If the function returns a struct, your program will crash mysteriously, even if you ignore the return value!
I really don’t see the need for using floating point in this case. Just do _speed/100. Oh yeah, C++ (not C) will warn on implicit conversion from float to int, so you’ll want to cast. Integer division already rounds toward zero, when the operands are positive. (Negative integer division rounding is inconsistent across platforms.)
Stupid HTML. I meant “#include math.h”, with angle brackets around math.h.
Pingback: Re: My code is haunted | OpenXcom
Just a quickie …
Is _speed/100.0 the same size as the argument to floor()?
I’m specifically concerned about float vs. double size — if that’s off, it won’t work.
C math functions can take any var size: http://www.cplusplus.com/reference/clibrary/cmath/floor/