0

In my application I am just dividing 50 by 3 I want to store the exact result value of this. If I use float it gives 16.666666 and if i use double then it gives 16.666667.

Actually,I am creating three labels inside a frame by dividing the height of the frame I am deciding the height of each label. so I f i do not get exact value it creates a gap between labels.if if i pass 60 then it works fine because 60/3 results 20 but if I pass 50 then there is a gap.

user1787741
  • 99
  • 2
  • 10
  • You want longer value than the above functions are already returning ? – Rajan Balana Feb 27 '13 at 05:23
  • What value do you expect to see? – JeremyFromEarth Feb 27 '13 at 05:24
  • 2
    The exact result of 50 ÷ 3 can't be stored finitely except as a ratio. Please go into more detail about your goal. – jscs Feb 27 '13 at 05:26
  • I am creating three labels inside a frame by dividing the height of the frame I am deciding the height of each label. so I f i do not get exact value it creates a gap between labels.if if i pass 60 then it works fine because 60/3 results 20 but if I pass 50 then there is a gap. – user1787741 Feb 27 '13 at 05:35
  • Please edit that information into your question. – jscs Feb 27 '13 at 05:37
  • As Josh already said, the rational number 50/3 *cannot* be represented as a `float` (or `double` or `long double` or ...), you *always* have rounding errors. But often the *accumulation* of rounding errors can be avoided. - If you show your code (or better: a small code example showing the behavior) it might be possible to give advise. – Martin R Feb 27 '13 at 06:54

3 Answers3

2

If you want to make your frame divide into three equal-height areas, then the height of your frame in pixels needs to be divisible by three. You can't display fractional pixels, they are not divisible; each height as measured in pixels needs to be an integer number.

  • Furthermore even though you CAN set fractional widths and points for window sizes, you should not because it usually ends up rendering everything in the slightly off-kilter view as fuzzy. In fact there is a method, CGRectIntegral(), exactly for the purpose of properly rounding off any non-integer values that creep into a CGRect view frame. – Kendall Helmstetter Gelner Feb 27 '13 at 08:05
  • But in the CGRectMake accepts parameters of type CGFloat, it should accept fractional value. – user1787741 Feb 27 '13 at 08:25
  • @user1787741: Yes, `CGRectMake` takes `CGFloat` parameters, but the fraction 50/3 is not a floating point number! Also the device has only discrete pixels, therefore I also think that `CGRectIntegral()` is the correct solution to this problem. – Martin R Feb 27 '13 at 08:53
1

The only way to store an "exact" value would be to create a class called "Rational" (or similar) and store the numerator and denominator of the fraction as separate ivars. Floats and doubles (or any literal computer representation for that matter) cannot store rational numbers with an infinite number of decimal places or transcendental real numbers.

The way to use the "Rational" class would be to store the numerator and denominator, and then apply the appropriate maths to these values (if you wish to propagate "exactness" through the program). The slightly easier way would be to display the rational number as numerator and denominator but use the float/double approximation for the underlying mathematics.

Ephemera
  • 8,672
  • 8
  • 44
  • 84
0
float t= (float)50/3;
long double t1= (long double)50/3;

NSLog(@"%.30f     %.30LF",t, t1);

produced output "16.666666030883789062500000000000 16.666666666666666666088425508008".

I would suggest you to go with long double which is not exact but precise enough.

Shashank
  • 1,743
  • 1
  • 14
  • 20
  • yes, I did this before, for NSLOG you can specify the precision value but how to pass this in CGRECTMake – user1787741 Feb 27 '13 at 08:23
  • You set precision to only view the number in the console. CGRectMake will have a precision of 7 decimal places as it takes CGFloat (typedef'd float) as argument. – Shashank Feb 27 '13 at 09:37
  • So no matter what you send CGRectMake method is going to consider up to 7 decimal places and I don't think sending float or double makes much of a visible difference. – Shashank Feb 27 '13 at 09:45