|
|
Up |
|
|
  |
Author: Dieter BritzDieter Britz Date: Oct 2, 2007 00:45
For a student assignment, students wrote something like this
(I have stripped it down and changed some symbol names):
program SincTest
implicit none
real :: x, SINX_X
read *, x
print '(" sinc(", f6.2, ") =", f8.4)', x, SINX_X(x)
end program SincTest
real function SINX_X (x)
real :: x, z, small
small = 1.0E-6
if (ABS(x) < small) then
z = 1.0
else
z = (SIN(x))/x
endif
! SINX_X = z
end function SINX_X
Output for input x = 1:
|
| Show full article (1.03Kb) |
|
| | 57 Comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Oct 2, 2007 01:29
Dieter Britz wrote:
(snip)
> real function SINX_X (x)
> real :: x, z, small
> small = 1.0E-6
> if (ABS(x) < small) then
> z = 1.0
> else
> z = (SIN(x))/x
> endif
> ! SINX_X = z
> end function SINX_X
(snip)
> How come there is a correct output at all?
It is certainly not obvious. Looking at the generated assembly code
in both cases might help understand what it is doing.
My guess is that the return value goes into a register, which
would normally be done by an assignment to SINX_X. Without that
assignment the value left in the register previously is returned.
|
| Show full article (0.82Kb) |
|
| | no comments |
|
  |
Author: nospamnospam Date: Oct 2, 2007 01:17
Dieter Britz wrote:
[example elided]
> How come there is a correct output at all?
I don't see that as being usefully answerable in this case. One can
speculate about how the compiler happens to assign addresses and
registers and how it might happen to get the "right" answer for that
particular compiler in this particular case, but I don't see a useful
generalization from this. It isn't as though there is a principle that
you can constructively use in coding.
Yes, it is possible for random coding errors to occasionally get the
"right" answer in particular cases on particular compilers. Plenty of
examples exist. In fact, it can be a bit unfortunate when the programmer
refuses to accept that there could be a programming error, since the
answer was "right"; I've run into that problem more than once.
I'll note, however, that I'd expect a good quality compiler to bitch
about the function never being assigned a value in this case. I have
seen compilers bitch about that.
|
| Show full article (1.19Kb) |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Oct 2, 2007 01:47
Richard Maine wrote:
(snip)
> Yes, it is possible for random coding errors to occasionally get the
> "right" answer in particular cases on particular compilers. Plenty of
> examples exist. In fact, it can be a bit unfortunate when the programmer
> refuses to accept that there could be a programming error, since the
> answer was "right"; I've run into that problem more than once.
> I'll note, however, that I'd expect a good quality compiler to bitch
> about the function never being assigned a value in this case. I have
> seen compilers bitch about that.
Another possibility, which I don't really believe, is that the
compiler figures out that z was supposed to be assigned to SINX_X.
The old "do what I mean, not what I say" logic.
-- glen
|
| |
| no comments |
|
  |
Author: TerenceTerence Date: Oct 2, 2007 02:32
As I once said (and disbelieved) on this Forum, the old F77 compiler I
use will allow a function to be called as a subroutine; in which case
nothing is done with the computed function result
In this particular case, the function returns an integer in AX (or
EAX); else the address of the value calculated by the function. If
called as a subroutine, the same happens.
So if there is no assignment of the function value to be returned
(before the END statment), what is in ax/EAX will be treated as the
computed result (or the address of same if not an integer).
Hence I think the corrected reason has already been suggested by
Dieter Britz.
|
| |
| no comments |
|
  |
Author: Rich TownsendRich Townsend Date: Oct 2, 2007 04:19
glen herrmannsfeldt wrote:
> Richard Maine wrote:
>
> (snip)
>
>> Yes, it is possible for random coding errors to occasionally get the
>> "right" answer in particular cases on particular compilers. Plenty of
>> examples exist. In fact, it can be a bit unfortunate when the programmer
>> refuses to accept that there could be a programming error, since the
>> answer was "right"; I've run into that problem more than once.
>
>> I'll note, however, that I'd expect a good quality compiler to bitch
>> about the function never being assigned a value in this case. I have
>> seen compilers bitch about that.
>
> Another possibility, which I don't really believe, is that the
> compiler figures out that z was supposed to be assigned to SINX_X.
> The old "do what I mean, not what I say" logic.
|
| Show full article (0.94Kb) |
| no comments |
|
  |
Author: Dieter BritzDieter Britz Date: Oct 2, 2007 05:14
Richard Maine wrote:
> Dieter Britz wrote:
> [example elided]
>> How come there is a correct output at all?
>
> I don't see that as being usefully answerable in this case. One can
> speculate about how...
|
| Show full article (1.57Kb) |
| no comments |
|
  |
Author: Michael MetcalfMichael Metcalf Date: Oct 2, 2007 06:41
>
> I guess one might be thankful that there was no complaint from the
> compiler, but on the other hand, if something slips through that is
> not supposed to, is it necessarily what one wants to slip through?
> I reckon not. So I call this a bug, not a feature.
> --
The standard does not require that a compiler detect that a function result
is not defined, but it's a very helpful warning. Intel 10 says:
C:\test\test.f90(9) : Warning: The return value of this FUNCTION has not
been
defined. [SINX_X]
and gives an incorrect result if nevertheless forced into execution. That's
much better that what PGI manages.
Regards,
Mike Metcalf
|
| |
| no comments |
|
  |
Author: Tim PrinceTim Prince Date: Oct 2, 2007 06:55
glen herrmannsfeldt wrote:
> Dieter Britz wrote:
>
> (snip)
>
>> real function SINX_X (x)
>> real :: x, z, small
>> small = 1.0E-6
>> if (ABS(x) < small) then
>> z = 1.0
>> else
>> z = (SIN(x))/x
>> endif
>> ! SINX_X = z
>> end function SINX_X
> (snip)
>
>> How come there is a correct output at all?
>
> It is certainly not obvious. Looking at the generated assembly code ...
|
| Show full article (1.54Kb) |
| no comments |
|
  |
|
|
  |
Author: Paul van DelstPaul van Delst Date: Oct 2, 2007 09:26
Dieter Britz wrote:
> For a student assignment, students wrote something like this
> (I have stripped it down and changed some symbol names):
>
> program SincTest
> implicit none
> real :: x, SINX_X
> read *, x
> print '(" sinc(", f6.2, ") =", f8.4)', x, SINX_X(x)
> end program SincTest
>
>
> real function SINX_X (x)
> real :: x, z, small
> small = 1.0E-6
> if (ABS(x) < small) then
> z = 1.0
> else
> z = (SIN(x))/x
> endif ...
|
| Show full article (2.75Kb) |
| no comments |
|
|
|
|