×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-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.

# converting if to math2

## converting if to math

(OP)
Hi guys! How to convert a expression with IF to only math?
I mean, how to transform the "if" in this following expression in a mathematical expression?

PROGRAM if_in_math
IMPLICIT none
INTEGER :: num

PRINT *, "Type a number"
IF(num.GE.0)THEN
PRINT *, num
END IF
END PROGRAM

I cannot !

François Jacq

### RE: converting if to math

#### Quote (j0zn)

how to transform the "if" ... in a mathematical expression?
If you mean with "mathematical expression" rather a logical expresiion then there are sme languages, which support this style. They need to support short-circuit evaluation and need to return in boolean context not only True or False - but the real value of an expression.
I think that this functional style is not supported by Fortran, because it can retrun in boolean concept only True or False.

But, for example in Perl you can do this

#### CODE

$num = 5; if ($num >= 0) {
printf("%d >= 0\n", $num); } # replacing above IF with logical expression$v = (($num >= 0) &&$num) || "";
printf("%s", \$v); 
which returns

#### CODE

5 >= 0
5 

The same output could be done in Python

#### CODE

num = 5

if (num >= 0):
print "%d >= 0" % num

# replacing above IF with logical expression
v = ((num >= 0) and num) or ""
print "%s" % v 

### RE: converting if to math

You have to play with the function called isign. Something like this

#### CODE

subroutine disp(n)
integer, intent(in):: n
! 3x3 = 9
character(len=9)::output
integer:: ibeg
! The inefficient bit
write(output, '(3X,I3,I3)') n, n
! Workout which one to use
ibeg = (isign(1, n) + 1) * 3 + 1
write(*, *) output(ibeg:ibeg + 2)
end subroutine disp

program main
call disp(-10)
call disp(0)
call disp(12)
end program 

### RE: converting if to math

Yeah, I was thinking along the lines of sign, too.

#### CODE

program noif
implicit none
integer num
character number(-1:1)*10 /3*'          '/

write(*,*) 'Type a number: '
write(number(1),'(i10)') num
write(*,*) number( (num+sign(1,num))/(abs(num)+1) )
end program noif 

### RE: converting if to math

(OP)
I would like some code like this following. Avoiding function. I would like to know what is the step to evolute doing it

### RE: converting if to math

Which function do you want to avoid. Why do you want to avoid it?

### RE: converting if to math

(OP)
I would like to avoid use "if". Some time my program used to contain a lot of "ifs"

### RE: converting if to math

Sometimes, IFs may be replaced efficiently by SELECT CASE constructs.

Another way is to use WHERE constructs which replaces in the same time DO loops and IF statements.

But for your example, the IF instruction is obviously the easiest way ! All the funny attempts to avoid it, as demonstrated by mikrom and xwb, have drawbacks like for instance an empty WRITE statement when num is <= 0 !

François Jacq

### RE: converting if to math

#### Quote (j0zn)

How to convert a expression with IF to only math?
...
I would like to avoid use "if".
Some time my program used to contain a lot of "ifs"

If in your opinion a program contains too many IFs, it is very naive or stupid to try to replace these IFs even with mathematical or logical constructs.
Even though you should be able to do this, it would be very likely that in the end your program will be much more incomprehensible than before.

So if you want to avoid too many IFs then change your programming style.

### RE: converting if to math

(OP)
mikrom, sometime it is really necessary. I am trying change and improve my programming style, even I'm beginner.
I am trying to do some analysis about some struture as a way to improve my programming style.
For example: I was thinking about how to chance it mathematics.

IF(lim_1>lim_2)THEN
interval = lim_1 - lim_2 + 1
lim_in = lim_2
ELSE
lim_in = lim_1
interval = lim_2 - lim_1 + 1
END IF

### RE: converting if to math

This time, it's easy :

#### CODE

interval = ABS(lim_1 - lim_2) + 1
lim_in = MAX(lim_2,linm_2) 

François Jacq

### RE: converting if to math

Yes in this simple case, you only compute a length and beginning point of an interval which could be easily expressed using mathematical functions ABS() and MIN().
Although in my opinion the original code with IF is better understandable then the mathematical expressions with ABS and MIN. But it's philosophical question what of the both is better - my opinion is that explicit is better than implicit

With changing programming style I thought about dividing your long programming task into short subtasks. Then for every subtask you can write a subroutine or a function. Keep your subroutines and functions slim and you shall not have to bother with too much code on one place and optionally with too many nested IFs.

For example your code given above could be placed into a short subroutines, which takes on input lim_1 and lim_2 and returns interval and lim_in :

#### CODE

subroutine get_interval(lim_1, lim_2, interval, lim_in)
implicit none
integer, intent(in) :: lim_1, lim_2
integer, intent(out) :: interval, lim_in

if (lim_1 > lim_2) then
interval = lim_1 - lim_2 + 1
lim_in = lim_2
else
lim_in = lim_1
interval = lim_2 - lim_1 + 1
end if
end subroutine get_interval 

The IF is now hidden in the subroutine. In the main program you only have to CALL the subroutine, so the code containing in the subroutine will not be irritating for you.

### RE: converting if to math

(OP)
FJacq your idea to this was very good! Thank you!!

mikrom I going to try from today use subroutines. It become the program pretty organized. Thank you again for your tips!

#### 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 Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

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

Here's Why Members Love Tek-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!