Re: Integer division
Re: Integer division
- Subject: Re: Integer division
- From: David Rowland <email@hidden>
- Date: Tue, 20 Nov 2007 08:24:00 -0800
On Nov 20, 2007, at 3:20 AM, Jonny Taylor wrote: Hi everyone,
Every now and then I catch a mistake in my code where I've been caught out by integer division. For example I was transcribing straight from an equation I'd worked out on paper, and wrote:
psi[l] * sqrt((l-m) * (l-m-1) / ((2*l+1) * (2*l-1)))
This will not do what I intended. In the extreme case, for large l ~= m the integer calculation for the sqrt ends up as zero. I should of course have forced the values to floating point to ensure it did what I wanted.
This is of course a very basic mistake, but one I admit to making every now and then - and it can prove hard to track down.
Is there a warning that I can enable in Xcode/gcc that would pick up on this? A warn-on-integer-division warning would generate some false positives, but I'd be happy to live with that if it caught the genuine errors. An smart warning that spotted when an integer result generated by division was cast to floating point would be even better, but I'd be impressed if that existed (it's a pretty complicated condition!).
Anyway, if anyone can suggest any relevant warnings I could enable, that would be great.
If you are using C++ there is a tedious but fruitful solution. Define your own class to replace "int", and overload the arithmetic operators. Then you can add tests for compile time and run time.
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden