Taylorh140 Posted March 27, 2020 Report Share Posted March 27, 2020 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. Quote Link to comment
Taylorh140 Posted March 27, 2020 Author Report Share Posted March 27, 2020 (edited) 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 March 27, 2020 by Taylorh140 spelling 1 Quote Link to comment
dadreamer Posted March 27, 2020 Report Share Posted March 27, 2020 (edited) 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. Untitled 1.vi 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 March 27, 2020 by dadreamer addition 1 Quote Link to comment
X___ Posted March 27, 2020 Report Share Posted March 27, 2020 This blog post: https://devs.wiresmithtech.com/blog/tag/labview/page/2/ Quote Link to comment
Taylorh140 Posted March 27, 2020 Author Report Share Posted March 27, 2020 @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. Quote Link to comment
X___ Posted March 28, 2020 Report Share Posted March 28, 2020 (edited) 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 March 28, 2020 by X___ Quote Link to comment
JKSH Posted March 28, 2020 Report Share Posted March 28, 2020 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. Quote Link to comment
X___ Posted March 28, 2020 Report Share Posted March 28, 2020 OK, my vision is in a state of exponential decay. For my defense, the second diagram shows DBL, as far as I can tell. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.