Step Function: Numerical Precision Matters

Click For Summary

Discussion Overview

The discussion revolves around the numerical implementation of a step function, particularly focusing on the challenges posed by numerical precision in computational languages like Fortran. Participants explore how to accurately define and compute the step function when dealing with very small values close to zero.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Goose describes the sensitivity of the step function to numerical precision, noting discrepancies when values are very small.
  • CompuChip provides a definition of the step function and questions the role of the small positive number, delta, in Goose's implementation.
  • Another participant suggests looking into Fourier transforms and the signum function for continuous solutions, while acknowledging the Gibbs phenomenon in numerical calculations.
  • Goose clarifies the need for a computational definition of the step function and expresses frustration over discrepancies in results compared to their advisor.
  • One participant recommends finding a Fortran package for arbitrary floating-point numbers or scaling values to avoid issues with small numbers.
  • Another participant inquires whether Goose is using double precision variables, which could handle smaller values before being treated as zero.
  • Further suggestions include defining an absolute minimum delta value for comparisons and addressing floating-point representation issues to avoid ambiguity.
  • Participants discuss the trade-offs between explicit branching definitions and analytical versions of the step function, with a caution about potential errors in the latter.

Areas of Agreement / Disagreement

Participants express various approaches to defining the step function and agree on the importance of numerical precision, but no consensus is reached on a single solution or method. Multiple competing views and suggestions remain present throughout the discussion.

Contextual Notes

Participants highlight limitations related to numerical representation in computing, including the handling of very small values and the implications of floating-point arithmetic. The discussion does not resolve these limitations or provide a definitive method for implementation.

Goose
Messages
5
Reaction score
0
Hi, Friends

How to numerically do with step function? I mean, this function is very sensitive to the precision. For example, f(x) is a step function. When x is greater than some very small positive number, \delta, the numerical answer is 1, which agrees with the expected value. But when x < \delta, the computer cannot tell it from 0, therefore the numerical result, f(x), equals to ZERO, which is not what I want.

Thanks for help

Goose
 
Physics news on Phys.org
Hi Goose.

I don't really see what kind of step function you are using and what it is good for. Usually a step function can be defined as [itex]\Theta_a(x) \equiv \Theta(x - a)[/itex] where
[tex]\Theta(x) = \begin{cases} 0 & \text{ if } x < 0 \\ \frac12 & \text{ if } x = 0 \\ 1 & \text{ otherwise} \end{cases}[/tex]
In numerical calculations this also works fine... where does delta come in?
 
If you want a continuous analytic solution you can try looking up articles on Fourier transforms dealing with the signum function. If you get a book dealing with various types of waveforms like sawtooth waves it will have a chance of containing that function (I can't remember it off the top of my head).

If you don't care about an analytic solution, just do what the poster described above and use some hard and fast boundary rules. The continuous analog exhibits the Gibbs phenomenon so you'll see weird effects around the boundaries but that's to be expected.
 
Thanks. My step function is the same as what CompuChip described. I know the exact analytical meaning of this function. But my question is how to define this function by Fortran or other computation language. In my research, I simply wrote subroutine using if statement as CoompuChip said. But the result is not desirable because computer doesn't know a very small number located in the neighbor of ZERO. For example, if [itex]x = 10^{-50}[/itex], the result is 0, not the expected value, 1. This difference affects my research a lot, and then my result always doesn't agree with my adviser's. Is there another way to construct step function?

Thanks again.
 
Ah, I see the problem now.
In that case I must leave answering your question to more qualified people. The only suggestions I can make are to a) find a package / routine / whatever it's called in Fortran which allows you to handle arbitrary floating point numbers and/or scientific notation; b) if you are only interested in values close to zero, scale everything by a factor 10^60 such that 10^{-50} is mapped to 10^{10}; c) check how your advisor did it.

But I hope people more experienced in numerical calculations will offer a good solution.
 
I agree with CompuChip's suggestion to scale the numbers, if you can do so. Or choose a different system of units, so that you are not dealing with such small values.

That being said, here is a question for Goose: are you using double precision variables? They would handle numbers as small as 10-307, or somewhere thereabouts, before treating the number as zero.
 
Good suggestions. Thank you all. I'll try it again.
 
Goose said:
Thanks. My step function is the same as what CompuChip described. I know the exact analytical meaning of this function. But my question is how to define this function by Fortran or other computation language. In my research, I simply wrote subroutine using if statement as CoompuChip said. But the result is not desirable because computer doesn't know a very small number located in the neighbor of ZERO. For example, if [itex]x = 10^{-50}[/itex], the result is 0, not the expected value, 1. This difference affects my research a lot, and then my result always doesn't agree with my adviser's. Is there another way to construct step function?

Thanks again.

If your using the explicit branching definition then you could probably do one of two things:

a) Define an absolute minimum delta value and always compare against that. Note that anything between the delta and zero will naturally using this definition be defined as zero and not as the number with regards to function behaviour.

b) Compare the floats memory specific value to a hardcoded value of 0. This is a few extra lines because of the intrinsics of how floating point numbers are represented with the mantissa and with things such as NaN and so forth. One hint I can say from experience is that -0 and +0 are both zero so you should always add +0 to the number before checking the footprint to avoid that ambiguity.

You asked if there was any other way to do this. The best way is to do what you've done and use an explicit branch condition. Another way is to use the analytical version. It however is prone to so much error that you would need to know a bit about numeric analysis to make the output stable at certain values.

Anyway you've probably got it by now so i'll leave it there
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 63 ·
3
Replies
63
Views
5K