# Fortran Help with a warning: Uninitialized Variable

#### Ottodomus

Hi, I am trying to run this program and I am having a warning:
"warning 298 - value X has been used without being given an initial value"

Here is my code: [moderator: code tags added]

Fortran:
real :: x
real :: arcsin
complex :: I = ( 0.0 , 1.0 )
print *, ' Enter the value of x '

if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I sqrt ( x ** 2 -1 ))
else
arcsin = ASIN ( x )
read *, arcsin
end if
print *, ' Result is: ' , x

end program

Last edited by a moderator:
Related Programming and Computer Science News on Phys.org

#### anorlunda

Mentor
Gold Member
Where does x get a value?

Also, you assign arcsin a value (you actually give it two values in two statements without use of the first value), but you never use the value.

I don't think Fortran is the problem. What programming experience or training do you have?

#### Ottodomus

Where does x get a value?

Also, you assign arcsin a value (you actually give it two values in two statements without use of the first value), but you never use the value.

I don't think Fortran is the problem. What programming experience or training do you have?
Hi, I've never said Fortran was the problem, I am an industrial designer fully pasionated with math, recently I came across with an old book of Fortran90/95 and gave it a try, and I have felt in love with the results, at this time I could "write" about 6 programs including the quadform solution, and I was thinking about the solution of some of the questions in trigonometry, that pointed me in one thing I had no idea: what about a complex solution? An inverse SIN of a number higher than 1?

That's why I am here bothering you and the others.

Thanks anyway for nothing

#### Mark44

Mentor
Thanks anyway for nothing
anorlunda's comment (below) was concerned with why you are getting the warning, so it was not "for nothing."
Where does x get a value?
Here is my code
Fortran:
real :: x
real :: arcsin
complex :: I = ( 0.0 , 1.0 )
print *, ' Enter the value of x '

if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I sqrt ( x ** 2 -1 ))
else
arcsin = ASIN ( x )
read *, arcsin
end if
print *, ' Result is: ' , x

end program
The reason for the warning you got was that you are using x in your if statement before you actually input a value for x. Getting the order wrong is a rookie mistake, and was exactly the reason that anorlunda asked about your programming experience, so please lose this attitude.

#### berkeman

Mentor
I am an industrial designer fully pasionated with math, recently I came across with an old book of Fortran90/95 and gave it a try
Ah, that explains why you are working with Fortran instead of another language. I kind of wondered about that.
and I have felt in love with the results
I know what you mean. My first semester in undergrad I was mainly thinking of majoring in ME, but took a programming class (Fortran, coincidentally). I loved the class, aced it, and changed to EE because it was so much fun.

Have you looked at any other languages yet? There are lots of options for learning to code, and advantages/disadvantages for each candidate language. I think we have a thread summarizing some of the options somewhere... I'll try to find it an provide a link. It's especially fun if you can find a language that has an easy way to do at least simple user graphical interfaces and graphical outputs...

Mentor

#### Ottodomus

Ah, that explains why you are working with Fortran instead of another language. I kind of wondered about that.

I know what you mean. My first semester in undergrad I was mainly thinking of majoring in ME, but took a programming class (Fortran, coincidentally). I loved the class, aced it, and changed to EE because it was so much fun.

Have you looked at any other languages yet? There are lots of options for learning to code, and advantages/disadvantages for each candidate language. I think we have a thread summarizing some of the options somewhere... I'll try to find it an provide a link. It's especially fun if you can find a language that has an easy way to do at least simple user graphical interfaces and graphical outputs...
Thanks Berkeman, I am actually interested in Java, I have some other books waiting for that, my older brother said that Java runs some applets with 2D input that could get some nice results, but at this time I wanted to solve first my issues in Fortran, don't get me wrong, but as I have solved some other programs I am interested in go beyond using that language, I have been reading also about C++, but it would be later.

#### anorlunda

Mentor
Gold Member
Try this. I struck the line that says "read *, arcsin" and inserted "read *, x" in an appropriate place. I also changed the final print from "print *, ' Result is: ' , x" to "print *, ' Result is: ' , arcsin" As @berkeman said, it is a rookie mistake to mix up both the identities of the variables and the sequence of the statements.

Code:
real :: x
real :: arcsin
complex :: I = ( 0.0 , 1.0 )
print *, ' Enter the value of x '
read *, x
if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I sqrt ( x ** 2 -1 ))
else
arcsin = ASIN ( x )

end if
print *, ' Result is: ' , arcsin

end program

#### Ottodomus

anorlunda's comment (below) was concerned with why you are getting the warning, so it was not "for nothing."

The reason for the warning you got was that you are using x in your if statement before you actually input a value for x. Getting the order wrong is a rookie mistake, and was exactly the reason that anorlunda asked about your programming experience, so please lose this attitude.
Ok what would be the order? I am trying here and there with no success at all.

#### Ottodomus

Try this. I struck the line that says "read *, arcsin" and inserted "read *, x" in an appropriate place. I also changed the final print from "print *, ' Result is: ' , x" to "print *, ' Result is: ' , arcsin" As @berkeman said, it is a rookie mistake to mix up both the identities of the variables and the sequence of the statements.

Code:
real :: x
real :: arcsin
complex :: I = ( 0.0 , 1.0 )
print *, ' Enter the value of x '
read *, x
if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I sqrt ( x ** 2 -1 ))
else
arcsin = ASIN ( x )

end if
print *, ' Result is: ' , arcsin

end program
Thanks it seems that is now running, now do you know how can I test if the results are ok? It seems that there's no calculator that actually could do imaginary or complex calculations.

#### berkeman

Mentor
It seems that there's no calculator that actually could do imaginary or complex calculations.
My HP42 and most scientific calculators can do complex calculations and conversions. You can get a free version of the HP42 for your smartphone by downloading the "Free42" app to your phone. But if you aren't familiar with "Reverse Polish Notation (RPN)" calculations, then the HP42 isn't for you.

You should be able to find online calculators to check your answers. Be sure to be careful about being consistent about using degrees or radians in your calculations, BTW.

#### Ottodomus

My HP42 and most scientific calculators can do complex calculations and conversions. You can get a free version of the HP42 for your smartphone by downloading the "Free42" app to your phone. But if you aren't familiar with "Reverse Polish Notation (RPN)" calculations, then the HP42 isn't for you.

You should be able to find online calculators to check your answers. Be sure to be careful about being consistent about using degrees or radians in your calculations, BTW.
I have a fantastic HP 41CV and the equivalent in my iPhone, and have also three more apps: Mathway, Calculator Plus, and GeoGebra graphing app, will give some of them a try to solve this, not that do not trust to Fortran... is why you know I am totally newbie in this language

#### berkeman

Mentor
I have a fantastic HP 41CV and the equivalent in my iPhone, and have also three more apps: Mathway, Calculator Plus, and GeoGebra graphing app
Very cool.

I didn't see complex math functions in the Operating Manual for the HP 41C, but I could have missed them:

http://www.decadecounter.com/vta/pdf/HP 41C Operating Manual.pdf

The HP 42S does have complex number calculations, although it's been decades since I used it for that:

http://www.hp41.net/forum/fileshp41net/manuel-hp42s-us.pdf

#### Attachments

• 48.5 KB Views: 152
• 311.8 KB Views: 150

#### Ottodomus

Well, I have another problem here, no matter what number I do assign to X the result is the same, something is wrong, here the complete code:

Code:
program complex_value
implicit none

! This program solves the complex value of inverse SIN when it is higher than 1

real :: x
real :: arcsin
complex :: I = ( 0.0 , 1.0 )
print *, ' Enter the value of x '
read *, x

if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I * sqrt ( x ** 2 -1 ))
else
arcsin = ASIN ( x )

end if
print *, ' Result is: ', arcsin

end program complex_value

#### Ottodomus

Whatever I do the result is the same:

#### Attachments

• 64.3 KB Views: 73

#### Mark44

Mentor
Ok what would be the order?
It's pretty simple.
1. Prompt the user to enter a number.
2. Input the number using a READ statement.
3. Use the number.

Your previous order was
1. Prompt the user to enter a number.
2. Use the number.
3. Input the number using a READ statement.
I hope you can see the difference.
Whatever I do the result is the same:
The screenshot is too small for me to read. Did you enter a number > 1? If so, your code is incorrect, since in that case, your variable arcsin will need to be complex.

Fortran:
real :: arcsin
.
.
.
if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I * sqrt ( x ** 2 -1 ))
To deal with complex numbers you need a different variable for values of x > 1. Note that if x == 1.0, there's no problem, so your if condition should be if ( abs(x) > 1.0) then ...

I would do this:
Fortran:
real :: arcsin_real
complex :: arcsin_cplx
.
.
.
if ( abs(x) > 1) then
arcsin_cplx = -I * log ( I * x + I * sqrt ( x ** 2 -1 ))
! Print the complex arcsine value
else
arcsin_real = asin(x)
!  Print the real arcsine value
end if
.
.
.
By the way, your formula for the arcsine of a complex number doesn't agree with the formula shown here: http://scipp.ucsc.edu/~haber/archives/physics116A10/arc_10.pdf. The formula is in section 2 of the PDF.

#### Ottodomus

It's pretty simple.
1. Prompt the user to enter a number.
2. Input the number using a READ statement.
3. Use the number.

Your previous order was
1. Prompt the user to enter a number.
2. Use the number.
3. Input the number using a READ statement.
I hope you can see the difference.

The screenshot is too small for me to read. Did you enter a number > 1? If so, your code is incorrect, since in that case, your variable arcsin will need to be complex.

Fortran:
real :: arcsin
.
.
.
if ( abs ( x ) >= 1 ) then
arcsin = -I * log ( I * x + I * sqrt ( x ** 2 -1 ))
To deal with complex numbers you need a different variable for values of x > 1. Note that if x == 1.0, there's no problem, so your if condition should be if ( abs(x) > 1.0) then ...

I would do this:
Fortran:
real :: arcsin_real
complex :: arcsin_cplx
.
.
.
if ( abs(x) > 1) then
arcsin_cplx = -I * log ( I * x + I * sqrt ( x ** 2 -1 ))
! Print the complex arcsine value
else
arcsin_real = asin(x)
!  Print the real arcsine value
end if
.
.
.
By the way, your formula for the arcsine of a complex number doesn't agree with the formula shown here: http://scipp.ucsc.edu/~haber/archives/physics116A10/arc_10.pdf. The formula is in section 2 of the PDF.
Thank you Mark! I have followed all your comments, yes my bad being so novice on this sorry for that!

I was following another's solution on the net, but yes it does make a sense not valuating the formula if equals to 1, I felt that could no affect the final solution, since no trying to give 1 as an input.

I have read the document you sent me, thanks! But I am lost in the "arg" part, to my limited understanding the argument is the variant in this case called Z the which is X, or am I wrong?

Still trying and will post the result, one more question, does this program needs to convert deg to rad in some part? or rad to deg? Just curious

#### Mark44

Mentor
I have read the document you sent me, thanks! But I am lost in the "arg" part, to my limited understanding the argument is the variant in this case called Z the which is X, or am I wrong?
Arg is the angle the complex number makes with the positive real axis. For example, if z = 1 + i, $|z| = \sqrt 2$ and $arg(z) = \frac \pi 4$.

Still trying and will post the result, one more question, does this program needs to convert deg to rad in some part? or rad to deg?
All of the inverse trig functions return values in radians.

#### Ottodomus

Arg is the angle the complex number makes with the positive real axis. For example, if z = 1 + i, $|z| = \sqrt 2$ and $arg(z) = \frac \pi 4$.

All of the inverse trig functions return values in radians.
Got it, thanks!

#### Ottodomus

Well, I think I have solved the program:

I am testing it with an online chart and it seems that is now properly working, Thanks!

#### Attachments

• 100.3 KB Views: 114

### Want to reply to this thread?

"Help with a warning: Uninitialized Variable"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving