Obtaining float from division between NSIntegers

10,480

Solution 1

Rather than converting integers to floats, you could just get floats in the first place:

CGFloat domandeFloat = [numDomande floatValue];
CGFloat corretteFloat = [numRisposteCorrette floatValue];
CGFloat voto = (corretteFloat / domandeFloat) * 10.0f
NSLog(@"float value is: %f", voto);

Solution 2

Simplest way is to do:

float voto = 10.0f * corrette / domande;

By making the first argument a float, you guarantee that the others will be promoted as well and that intermediate and final results will not suffer truncation.

You could achieve a similar result by casting corrette to a float but I tend to prefer simplicity where possible.

Solution 3

NSInteger does not have a method called floatValue. NSInteger is just an int. Instead, the solution would be:

CGFloat domandeFloat = [[NSNumber numberWithInt: numDomande] floatValue];
CGFloat domandeFloat = [[NSNumber numberWithInt: numRisposteCorrette] floatValue];
CGFloat voto = (corretteFloat / domandeFloat) * 10.0f;

Solution 4

Try to convert the NSIntegers to a float type first:

float voto = (float)corrette*10/(float)domande;
Share:
10,480
BigCola
Author by

BigCola

Updated on June 05, 2022

Comments

  • BigCola
    BigCola almost 2 years

    I have two NSInteger variables called "domande" and "corrette". I have to execute this operation with them: corrette*10/domande. I want the result to be a float variable, so I declared a "voto" variable as so: "float voto = corrette*10/domande;" . When I output the value of "voto" with NSLog I get an approximated value of the result followed by ".000000".

    Here's the code:

    NSInteger domande = [numDomande integerValue];
    NSInteger corrette = [numRisposteCorrette integerValue];
    float voto = corrette*10/domande;
    NSLog(@"float value is: %f", voto);
    

    When I assign to "domande" a value of 7, and to "corrette" a value of 4: voto=5.000000 Instead it should be voto=5.71...

    How can I have the division return not an integer type converted to float, but directly a float type?

  • medvedNick
    medvedNick over 11 years
    [corrette floatValue] is not right - you can not send a message to not id type. Maybe you meant "(float)corrette" ?
  • Asciiom
    Asciiom over 11 years
    Ah yes, I meant to cast :) I was a bit too quick. Thanks!
  • Richard J. Ross III
    Richard J. Ross III over 11 years
    That's not casting 10 to a float, it's using the nearest binary representation of 10 as a floating point number. No conversion is done, and thus no cast.
  • medvedNick
    medvedNick over 11 years
    well, yes, this "cast" means "change type of 10 from int to float so it will be float from the beginning"
  • Richard J. Ross III
    Richard J. Ross III over 11 years
    But that's not what a cast is in C, fundamentally the two are quite different.
  • PakitoV
    PakitoV over 5 years
    Actually the most correct way would be to use numberWithInteger as the source numDomande is a NSInteger, not an int