×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

#### Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# I need 3.6 not 3.5999

 Forum Search FAQs Links MVPs

## I need 3.6 not 3.5999

(OP)
Hi all,

I am making a code using fortran77.
I input 3.6. Strangely when I call the value, it gives me 3.5999.
Do you know how to fix this problem?
Please help me.

Thank you.

### RE: I need 3.6 not 3.5999

Is this for school?

### RE: I need 3.6 not 3.5999

(OP)
No, it is not. I am working on a code for my research.
I'm just biginner of Fortran.
Thank you.

### RE: I need 3.6 not 3.5999

So, do you need numerically correct, or cosmetically correct?

### RE: I need 3.6 not 3.5999

(OP)
I need to use the value to calculate another value. I defined the value of 3.6 to "CA". The problem is that "CA" is called as 3.59999. It gave me wrong answer because the calculation is conducted with 3.5999.

### RE: I need 3.6 not 3.5999

I think you have a more serious problem of why you can get a "right" answer at 3.59999, but a "wrong" answer at 3.5999.  The uncertainty in any real world scenario will swamp out this trivial precision error.

You need to explain more about your problem.

### RE: I need 3.6 not 3.5999

(OP)
Sorry about bothering you. I mean I want to use 3.6. But, during the computation, 3.5999 is used instead of 3.6 even though I input 3.6 as an value.

### RE: I need 3.6 not 3.5999

The issue is that you cannot get exactly 3.6, because it's not a binary number.  Have you checked the numerical precision of your variables?  Do you really expect people to know what you did in your program without a listing?

### RE: I need 3.6 not 3.5999

(OP)
I appreciate your explanation.
I understand what you mean.
Frankly, I don't know how to check the numerical precision.
Could you let me know how to check?

Thank you.

### RE: I need 3.6 not 3.5999

Have you read the manual?  What did the prof say?

### RE: I need 3.6 not 3.5999

(OP)
Yes, I'm reading the manual. It is quite difficult to me. It is my first coding in my life.
I'm learning and, at the same time, coding the program.

### RE: I need 3.6 not 3.5999

It is a long time since I worked in FORTRAN - it was still called FORTRAN IV when I learnt it!  Did you declare the TYPE for your variable CA?  Try declaring it as DOUBLE PRECISION or whatever the equivalent is in FORTRAN 77. If you have enough 9's after the 5 the value becomes indistinguishable from 3.6

Katmar Software
Engineering & Risk Analysis Software
http://katmarsoftware.com

### RE: I need 3.6 not 3.5999

Another trick is to multiply by 36 and then divide the answer by 10.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein

### RE: I need 3.6 not 3.5999

Sorry Cajun, but that is nonsense!

If you work with integer variables and divide 360 by 10, yes you would get 36, no problem.

But divide 36 by 10, with integer variables would give 3, with real variables (whether single or double precision) you would get something like 3.599999 or 3.600001, but never precisiely 3.6

### RE: I need 3.6 not 3.5999

==> Sorry Cajun, but that is nonsense!
Not, it is a quite valid technique to substantially reduce floating point roundoff error.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein

### RE: I need 3.6 not 3.5999

Cajun,

Real variables in fortran are stored as a mantissa and an exponent.

A single precision real variable is 4 bytes = 32 bits, of those 32 bits, 24 store the mantissa and 8 store the exponent.

Thus changing the value of the exponent will not change the accuracy of computation in fortran!

In fact by including the additional computation of dividing by 10 you are introducing more round off errors not reducing them!

### RE: I need 3.6 not 3.5999

Believe what you will, but I know the actual code being generated by many, not all, but many FORTRAN compilers.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein

### RE: I need 3.6 not 3.5999

So its not stored as a mantissa and an exponent?

### RE: I need 3.6 not 3.5999

You still haven't posted a listing.

### RE: I need 3.6 not 3.5999

(OP)
IRstuff,

The code is quite long. How can I post it?

### RE: I need 3.6 not 3.5999

see:
Step 3
Attachment
at bottom of the dialog box.

You need to figure out what to extract.  How do you expect anyone to debug your code, sight unseen?

Why is your manager giving you such a complicated task?

### RE: I need 3.6 not 3.5999

(OP)
Integer BURB
DOUBLE PRECISION BUR

BUR=3.6
BURB=1.0+(BUR+140.0)/0.2

In this code, 'BURB' is calculated as 718.
I want 719.

### RE: I need 3.6 not 3.5999

You should already have the educational background to understand this, so I will ask again, what is this assignment for?

### RE: I need 3.6 not 3.5999

(OP)
I want to get right value for 'BURB'. I understand you about the binary numbers.
I want to use 'Burb' as a indicator of interation. That's it.

### RE: I need 3.6 not 3.5999

As an engineer, you've already gotten everything you need to solve your problem in the postings above.

### RE: I need 3.6 not 3.5999

Welcome to the delights of mixed-mode arithmetic.  It causes tears (and blood, and sweat) in every language I have ever worked in.

### RE: I need 3.6 not 3.5999

I believe Denial is correct. If you are leaving the REAL to INTEGER conversion up to the compiler then you are asking for trouble, in any language or compiler. Rather do it under your own control. See the post by johnhors in thread545-95464: Rounding REAL numbers.

Do something like

Integer BURB
DOUBLE PRECISION BUR, BURA

BUR=3.6
BURA=1.0+(BUR+140.0)/0.2
BURB=INT(BURA+0.5)

Katmar Software
Engineering & Risk Analysis Software
http://katmarsoftware.com

### RE: I need 3.6 not 3.5999

It's simpler than that.  BUR can simply be defined as 18 to start with and avoid both the decimal fraction and the division by 0.2.  Likewise, the constant 140/0.2 should simply be written as 140*5 or just 700.  At that point, one wonders what the exact purpose of the additional 1 is.

Just an iota of research would have made it plainly obvious that the coding is at fault.  However, you need to review what the purpose of the equation as written is, since it's both cumbersome and questionable in intent.

### RE: I need 3.6 not 3.5999

The number is being rounded down.  Use nint to get the nearest integer.

#### CODE

BURB=NINT(1.0+(BUR+140.0)/0.2)
If you are getting timing problems (say you need to compute this a few million times, use

#### CODE

BURB=NINT(1.0+(BUR+140.0)*5.0)

### RE: I need 3.6 not 3.5999

IRstuff - I agree with your comments in your last post. I would also question the OP's statement "I am making a code using fortran77." as fortran77 is now obsolete, I use silverfrost ftn95, they no longer support their ftn77 product, in the gnu community f77 is also no longer supported having moved on to g95 or gfortran.

### RE: I need 3.6 not 3.5999

Hello:
Just try this for Format F3.1, this should take care of it

Thanks

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

### Reply To This Thread

#### Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login

#### Resources

eBook - Functional Prototyping Using Metal 3D Printing
Functional prototypes are a key step in product development â€“ they give engineers a chance to test new ideas and designs while also revealing how the product will stand up to real-world use. And when it comes to functional prototypes, 3D printing is rewriting the rules of whatâ€™s possible. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.