Jump to content

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. 

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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___

Share this post


Link to post
Share on other sites
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.

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.


  • Similar Content

    • By David Akinpelu
      I am implementing a TCP connection between MyRio(client) and Python(server). The goal is to send data from the server to the client, perform some calculations, and send the result to the server. I need to keep changing the data being sent to the client. I noticed the first data works fine and the result returned to the server, but the client could not read subsequent data. I debugged and was able to detect that the error is coming from the first read function is the "Reading values subvi"  but I don't know how to correct it. How do I go about it?
      I have attached the files below.
      Reading Unknown Bytes from TCP.vi Second_client.vi SimpleServer.py
    • By Deon
      The Threshold Hysteresis VI allows an input (for example, a sensor) to drift out of an inner limit without generating an invalid flag, but if it drifts outside an outer limit it then becomes invalid. For the reverse, when the input crosses back into the outer limit it remains invalid until the value falls inside the inner limit.
      For simplicity, only one input parameter is provided for both the inner & outer limits, and is negated for the lower limit, as generally tolerance limits are equidistant from the nominal.
    • By Deon
      View File Threshold Hysteresis v1.0 LV8.6.1
      The Threshold Hysteresis VI allows an input (for example, a sensor) to drift out of an inner limit without generating an invalid flag, but if it drifts outside an outer limit it then becomes invalid. For the reverse, when the input crosses back into the outer limit it remains invalid until the value falls inside the inner limit.
      For simplicity, only one input parameter is provided for both the inner & outer limits, and is negated for the lower limit, as generally tolerance limits are equidistant from the nominal.
      Submitter Deon Submitted 11/10/2014 Category General LabVIEW Version License Type  
    • By jossie
      Hello I am quite new to labview and I have some questions regarding labview. We have came up with a simulation build in labview as shown in the folder. However, we will be making use of Myrio to connect current and voltage sensors to send the data to labview when labview is running. We are currently having difficulties trying to search if it is possible to send the wave files and the popout messages ( messagebox.vi and Player2Wins.vi) to NI Dashboard for Labview in Ipad by using myrio. Is it possible for myrio to store wave files and the additional VIs and sending these to NI Dashboard for Labview? 
       
      Otherwise, it is possible for Labview to send the entire program.vi, messagebox.vi and Player2Wins.vi to cloud and people are able to see the GUI in program.vi remotely? ( We have found information like Labview web services etc. We hope that people are able to view our GUI and press the " Press start" button as well as closing the popout messages in a website not from our local network). 
       
      I sincerely apologize if what I am asking does not make sense to people who are reading this. I hope that someone will help me with this as it is very difficult for me to do and I need some guidance from people who are familiar with these. Thanks for taking your time to read and your reply. 
      Simulation_Build_(3).zip Pop-up_Message.vi Player2Wins.vi
    • By edupezz
      Hello, i created a new tips repository in:
      https://edupez.com/
      English and Portuguese
×
×
  • Create New...

Important Information

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