Help with a warning: Uninitialized Variable

  • Fortran
  • Thread starter Ottodomus
  • Start date
  • Tags
    Variable
In summary, the conversation discusses a warning that the programmer received while trying to run a Fortran program. The warning was about a value, X, being used without being given an initial value. The code provided by the programmer includes an if statement that uses X before it is given a value. The conversation also touches on the programmer's background and experience with programming, as well as their passion for math and their love for the results of their Fortran programs. There is also mention of looking into other programming languages for possible advantages and a link to a thread discussing high-level programming languages.
  • #1
Ottodomus
19
7
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:
Technology news on Phys.org
  • #2
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?
 
  • #3
anorlunda said:
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
 
  • #4
Ottodomus said:
Thanks anyway for nothing
anorlunda's comment (below) was concerned with why you are getting the warning, so it was not "for nothing."
anorlunda said:
Where does x get a value?
Ottodomus said:
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.
 
  • Like
Likes berkeman
  • #5
Ottodomus said:
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. :smile:

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...
 
  • Like
Likes Ottodomus
  • #7
berkeman said:
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. :smile:

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.
 
  • Like
Likes berkeman
  • #8
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
 
  • Like
Likes Ottodomus and berkeman
  • #9
Mark44 said:
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.
 
  • #10
anorlunda said:
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.
 
  • #11
Ottodomus said:
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.
 
  • #12
berkeman said:
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
 
  • Like
Likes berkeman
  • #13
Ottodomus said:
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. :smile:

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

upload_2019-1-24_13-2-33.png


upload_2019-1-24_13-2-8.png
 

Attachments

  • upload_2019-1-24_13-2-8.png
    upload_2019-1-24_13-2-8.png
    11.4 KB · Views: 397
  • upload_2019-1-24_13-2-33.png
    upload_2019-1-24_13-2-33.png
    65.1 KB · Views: 409
  • Like
Likes Ottodomus
  • #14
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
 
  • #15
Whatever I do the result is the same:
 

Attachments

  • Captura de pantalla (34).png
    Captura de pantalla (34).png
    16.6 KB · Views: 306
  • #16
Ottodomus said:
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.
Ottodomus said:
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.
 
  • Like
Likes Ottodomus and berkeman
  • #17
Mark44 said:
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
 
  • #18
Ottodomus said:
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##.

Ottodomus said:
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.
 
  • Like
Likes Ottodomus
  • #19
Mark44 said:
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!
 
  • Like
Likes berkeman
  • #20
Well, I think I have solved the program:

Captura de pantalla (36).png

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

Attachments

  • Captura de pantalla (36).png
    Captura de pantalla (36).png
    23.9 KB · Views: 365
  • Like
Likes berkeman

What does it mean when I receive a warning about an uninitialized variable?

When you receive a warning about an uninitialized variable, it means that you have attempted to use a variable in your code without first assigning a value to it. This can lead to unexpected results or errors in your program.

Why is using an uninitialized variable a problem?

Using an uninitialized variable is a problem because the computer does not know what value to assign to it. This can lead to unpredictable behavior in your code and make it difficult to troubleshoot errors.

How can I fix the warning about an uninitialized variable?

To fix the warning about an uninitialized variable, you need to make sure that you have assigned a value to the variable before using it in your code. You can do this by initializing the variable with a default value or by assigning it a value based on user input or calculations.

Can an uninitialized variable cause my program to crash?

Yes, an uninitialized variable can cause your program to crash. If the variable is being used in a critical part of your code, such as a conditional statement or a mathematical operation, the program may not be able to handle the lack of a value and crash.

How can I prevent uninitialized variables in my code?

To prevent uninitialized variables in your code, you should make sure to initialize all variables before using them. You can also use good coding practices, such as assigning default values and checking for user input, to ensure that all variables have a value before they are used.

Similar threads

  • Programming and Computer Science
Replies
3
Views
701
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
931
Replies
2
Views
740
  • Programming and Computer Science
Replies
2
Views
965
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
856
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top