Jump to content

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. 

 

Link to comment

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.

https://github.com/taylorh140/LabVIEW-Float-Utility

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

 

Edited by Taylorh140
spelling
  • Like 1
Link to comment

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.

Example_VI_BD.png.6ac1ace83cc2bba29ef6fb38710e2471.png

Untitled 1.vi

2020-03-28_0-37-12.jpg.2aad795f855df3d3a3b5c527efac31b8.jpg

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.

Edited by dadreamer
addition
  • Like 1
Link to comment

@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) 

image.png.619acf0a1d2652f38e0a006002a078f7.png

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:

image.png.833a370dcbe68acb7fc4e2bb09baad9d.png

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.

 

Link to comment

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).

 

Numeric Precision Test.png

Edited by X___
Link to comment
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.

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.