# Floating Point Steps

## Recommended Posts

So I have a serious question. Does LabVIEW have a way (built in or library) of calculating the next larger or next smaller floating point value.

C standard library "math.h" has two functions:

nextafter, and nexttowards.

I have put together a c function: that seems to do the trick nicely for single floats (well only for stepup):

```#include <math.h>
#include <stdint.h>

uint32_t nextUpFloat(uint32_t v){
uint32_t sign; = v&0x80000000;

if (v==0x80000000||v==0){ //For zero and neg zero
return 1;
}

if ((v>=0x7F800000 && v<0x80000000)||(v>0xff800000)){ //Check for Inf and NAN
return v; //no higher value in these cases.
}

sign = v&0x80000000; //Get sign bit

v&=0x7FFFFFFF; //strip sign bit
if(sign==0){
v++;
}else{
v--;
}

v=v|sign; //re merge sign
return v;
}```

I could put this in labVIEW, but these things are tricky and there are some unexpected cases. So its always better to use a reference.

So I put something together. It implements NextUp and NextDown.

I was thinking it would be nice to have a approximation compare that took a number of up/down steps for tolerance. Let me know if you think there is any interest.

If your curious please check it out, and make sure I don't have any hidden Bugs. 😁

Edited by Taylorh140
spelling
• 1

I might be wrong, but I've never seen a LabVIEW built-in and exported function like nextafter or nexttowards in extcode.h or inside any Managers. But there's a WinAPI implementation according to MSDN: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/nextafter-functions?redirectedfrom=MSDN&view=vs-2019

So you might try to use those func's from msvcrt.dll by the means of CLFNs.

Hope, it helps somehow. Oh, and by the way, for Linux you could use libm.so and libSystem.dylib for macOS to call the mentioned functions, if you're not on Windows.

• 1

@X___ So.. the article is a little misleading in its description of epsilon. For a single float the first step from 0 to the next value is 1.401E-45 which is much smaller than epsilon (even for a double)

In reality epsilon just represents a guaranteed step size minimum you can expect between the range of 0-1. Its calculated by getting the step size after one:

I know that it doesn't count for larger values from experience. If you add one too a double it will only increment for a while before it cant represent the next gap.

But I was curious what the epsilon was too. So hopefully that helps.

I am not sure what you are saying: your code shows exactly what the blog post (and the linked Stack Overflow thread) is saying.

The machine epsilon's definition is clear about what it means. It is not what you are looking for, granted, but that was not the part I was sort of meaning to focus on in the blog post. Sorry about the confusion.

The VI below does essentially was McNally talked about in his blog, and you can see that for a Numeric input of, say 1E-34, you get a next step of 2.138211768073757E-50, so your next step DLL is fooling you (or is it?) if it reports 1.4...E-45 as the next value after 0 (obviously, a Numeric value of zero will not get you any useful answer).

Edited by X___
12 hours ago, X___ said:

your next step DLL is fooling you (or is it?) if it reports 1.4...E-45 as the next value after 0

Taylorh140's result is correct. He was talking about SGL values.

When you're using a 32-bit SGL, 1.4013E-45 is the smallest possible positive value. In other words, 1.4013E-45 is the next SGL value after 0.

When you're using a 64-bit DBL, 4.94066E-324 is the smallest possible positive value.

OK, my vision is in a state of exponential decay. For my defense, the second diagram shows DBL, as far as I can tell.

• ### Similar Content

• #### Web Service HTTPS

By CIPLEO,

• 0 replies
• 638 views
• #### Transparent front panel

By Harris Hu,

• 7 replies
• 849 views
• #### The sub-panel is always at the very top level when refreshed

By Harris Hu,

• 474 views
• #### Test Sequence Editor for LabVIEW Development

By xiongxinwei,

• 2,091 views
• #### How to convert structural data between Labview and TIA Portal(siemens s7)

By Harris Hu,

• 8 replies
• 2,140 views
×