anuvaramban
anuvaramban

Reputation: 151

How to set Integer and Fractional Precision independently?

I'm learning Fortran(with the Fortran 2008 standard) and would like to set my integer part precision and decimal part precision for a real variable independently. How do i do this?

For example, let us say that i would like to declare a real variable that has integer part precision as 3 and fractional part precision as 8.

An example number in this above specification would be say 123.12345678 but 1234.1234567 would not satisfy the given requirement.

Upvotes: 1

Views: 803

Answers (2)

Jtox
Jtox

Reputation: 1

I ended up writing a function for this in order to use floating points with the .gt./.lt./.ge./.le./.eq. operators without actually modifying the floating points.

function PreciseInt(arg1, arg2) Result(PreciseInt)
  real*8     arg1       !Input variable to be converted
  integer*4  arg2       !Input # for desired precision to the right of the decimal
  integer*4  PreciseInt !Integer representing the real value with desired precision

  PreciseInt = idnint(arg1 * real(10**arg2))
end function

Upvotes: 0

Fortran real numbers are FLOATING point numbers. Floating point numbers do not store the integer part and the decimal part. They store a significand and an exponent.

See how floating point numbers work http://en.wikipedia.org/wiki/Floating-point_arithmetic There is usually one floating point format which your CPU uses and you cannot simply choose a different one.

What you are asking for is more like the FIXED point arithmetic, but modern CPUs and Fortran do not support it natively. https://en.wikipedia.org/wiki/Fixed-point_arithmetic

You can use them in various libraries (even probably Fortran) or languages, but they are not native REAL. They are probably implemented in software, not directly in the CPU and are slower.

Upvotes: 2

Related Questions