So I haven't posted here much if at all before today. Hello Programming forum!
I've been working through Project Euler to shake off the rust and also to learn Objective-C, although it's been almost entirely C code so far with the exception of using NSString for string handling. Some thoughts on data types.
- I've started using long (64 bit) instead of int (32 bit) for everything. I'll use char for characters and BOOL for booleans but even though those are implemented as integer types, I'm not thinking of them as integers and the typing makes my intent clearer, but otherwise everything is a long. I don't like the idea of having to think about different types of integer so everything is 64 bits. I could see making an exception for very large arrays where memory capacity becomes a concern but in 99% of cases, using multiple integer types and remembering which one I used where feels like a waste of my time.
- Similarly for float, double. I just never use float, always use double. Or is long double the new double and I should go 128 bit all the way for floating point? And as an aside on the type naming, I'd prefer if the names were int32, int64 (or uint32, uint64 if you want unsigned) and float64, float128, etc. It doesn't matter so much to me since I'm only using one type of int, one type of float, but having to remember which types are implemented using which number of bits is kind of annoying. I realize those mappings are system dependent but if I'm writing code that expects an integer to be 64 bits, I like to know that it's actually 64 bits.
- I wish a class similar to Java's BigInteger existed in Objective-C for those rare cases when 64 bit isn't enough (ok so I still can't get completely away from thinking about multiple integer types, but with a 64 bit standard type the occasions when I need to become pretty rare). As far as I know there isn't one, but from
this discussion on Stack Overflow I found
JKBigInteger which is an Objective-C wrapper class using a C library that does what I want. Nice to have.
- I haven't made one yet but I think I'd like an Objective-C rational number class, and I think I'll implement it using JKBigInteger for numerator and denominator. My rationale is that if I'm going to the trouble of using integer ratios instead of floating point, it must be a situation where I value precision over performance and I don't want to have to think about the possibility of overflowing a 64 bit int numerator / denominator (and then using 64 bit num/denom approximations from there).
Thoughts on any of this?