Using a data type other than int as an index value in C ++ for a loop

I accept user input as a floating point value.

This value may increase to "10 raised to a power of 18".

The next step involves finding all the divisors of this number. for which I do the following:

      for(i=2; i<=n/2 ; i++)
      {
        if(n%i==0) 
           v.push_back(i);
      }

Here n is the number entered by the user.

The problem is that n is a float and uses it if the loop index causes its value to be limited to '10 raised to power 9 '

Therefore, is there any way to use a data type other than int to use the values โ€‹โ€‹of the range '10 applied to power 18 '?

+3
source share
4 answers

unsigned long long, 2 64 10 19

, 64- .

+3

( long long int), , " " . , , . , , .

: http://en.wikipedia.org/wiki/IEEE_754-2008

:

int main(void)
{
    float i = 16777217.0f;
    printf("i = %f\n", i);
    i++;
    printf("i+1 = %f\n", i);
}

w/32-bit float :

i = 16777216.000000
i+1 = 16777216.000000

, : , -, , ?

for(float f; f < 20000000; ++f) 
{
    // do stuff
}
+2

I think a long double should be significant.

0
source

Of course you can use other data types for the loop, use any of the types listed here

0
source

All Articles