@Peter87 Thanks! That was the problem!
@coder777 One meter(lets say on display) is 10000 in memory. 1.5m is 15000 in memory. With this way I can have precision with integers. I hope you get it why one meter is 10000
I would prefer to do the calculations with doubles including any scaling for the screen, then cast them if necessary to int. Do this with functions that have meaningful names, the programs intent will be much clearer. The code is confusing at the moment. Code should be easy to understand, even for those that may not know much about the topic. I am an Engineering Surveyor, and I found it confusing until you explained what you were doing.
Use the constant value of 25.4mm per inch to convert between metric and imperial, multiplying or dividing as necessary. Values like 32'808'399LL are not exact.
I prefer int instead double because value it stored in EEPROM(Arduino). That's the reason why one meter is 10000 in memory. Everything works fine for metric system, but imperial doesn't work as it should. It's confusing me.
You can store it as an int and still cast to double when doing division. Otherwise it will do integer division, like the others said.
value * (static_cast<double>(one_m2ft) / one_km)
I fixed it by adding (float) in macro functions. Problem was that Imperial units aren't round as metric units(eg 1mi has 5280ft; 1km has 1000m). What's why conversion didn't work.
Now I can convert without any problem. Only I lose about 15m with every conversion. That's not big problem because user never should change system unit. But I added that option.
I'm simplifying a bit (I think), but static_cast<double>(variable) is the equivalent of doing (double)variable.
One reason to prefer static_cast<T>(variable) as opposed to (T)variable is it is much easier to search for, after the fact (you can easily search "cast"). It's very hard to search for a C-style cast, making refactoring harder.
You mentioned you get inaccuracies of around ~15m. Try doing (double) instead of (float), though I'm not sure if it will make a big difference, but doubles have double the precision of floats on most platforms.
I prefer int instead double because value it stored in EEPROM(Arduino). That's the reason why one meter is 10000 in memory. Everything works fine for metric system, but imperial doesn't work as it should. It's confusing me.
This sounds like an XY problem. Is it an actual requirement to store 1 metre as 10000 in memory, or is that your solution to a problem that is at the moment unknown to us.
Why is the value stored in EEPROM? What are these metre values going to be used for?
Arduino seems to have a double type (and the cast function int() ), can you explain why it needs to be an int everywhere? I am having difficulty as to seeing why using double for the calculations, then casting to int as the very last step would not work.
Isn't "Arduino" a language of its own that merely has loaned some bits from C and C++?
Wiki says: "8-bit hardware". A "double" or "long" can't be much more there than "smaller" data types.
You should have mentioned the Arduino up front and thus save yourself from advice that does not apply to your system.
Can I program the Arduino board in C?
In fact, you already are; the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). All standard C and C++ constructs supported by avr-g++ should work in Arduino. For more details, see the page on the Arduino build process.
One reason to prefer static_cast<T>(variable) as opposed to (T)variable is it is much easier to search for, after the fact (you can easily search "cast"). It's very hard to search for a C-style cast, making refactoring harder.
Yeah, that's a good point. I've probably only done it once or twice to fix a problem I didn't create. The thing about C++ is that well-written C++ requires less casting than C, so it is less of an issue.
Maybe, because static_cast is so ugly and so relatively hard to type, you're more likely to think twice before using one? That would be good, because casts really are mostly avoidable in modern C++.
It's more than just the potential of code searching; he's saying it increases readability in making sure the intention is clear, and static_cast is more restrictive than (oldstyle) cast.
But for something simple like casting an int to double for division... it hardly makes a difference, so not really worth the time to worry about it.