silentcontributor
silentcontributor

Reputation: 85

Modulo for doubles

I need to work out if a massive integer (I mean 20 digits...) is prime.

I'm trying to use the brute-force method, but (of course) I need to use doubles to contain the original number. However, the modulo operator (%) is an integer operator - thus it is useless to me!

Upvotes: 0

Views: 716

Answers (3)

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59473

Since the double data type is stored as a fractional value scaled to some power of two, and since it only has a precision of 15 decimal digits, a 20-digit number stored as a double will always be divisible by two, and therefore, is not prime.

Upvotes: 3

user25148
user25148

Reputation:

Are you looking for fmod in the C standard library? Or possibly fmodlfor long double?

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941635

That's not possible, a double only has 15 significant digits. Look for an implementation of a BigInt class. C specific is discussed here.

Upvotes: 18

Related Questions