Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:15:03+00:00 2026-06-08T15:15:03+00:00

I would like to define a function which returns the string NaN or sprintf(%g,val)

  • 0

I would like to define a function which returns the string “NaN” or sprintf(“%g”,val) depending on whether val is a string or a numeric value. Initially I was trying to test if val was defined (using the gnuplot “exists” function) but it seems that I cannot pass any undefined variable to a function (an error is issued before the function is evaluated). Therefore: is there a way to test inside a function whether the argument is a string or numeric?

I search for a function isstring which I can use somehow like

myfunc(val)=(isstring(val)?"NaN":sprintf("%g",val))

The goal is to output the values of variables without risking errors in case they are undefined. However I need it as a function if I want a compact code for many variables.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:15:05+00:00Added an answer on June 8, 2026 at 3:15 pm

    Gnuplot doesn’t really have the introspection abilities that many other languages have. In fact, it treats strings and numbers (at least integers) very similarly:

    print "1"+2  #prints 3
    a=1
    print "foo".a  #prints foo1
    

    I’m not exactly sure how this is implemented internally. However, what you’re asking is very tricky to get to work.

    Actually, I think your first attempt (checking if a variable exists) is more sensible as type-checking in gnuplot is impossible*. You can pass the variable name to the function as a string, but the problem is that you don’t seem to have a handle on the value. All seems lost — But wait, gnuplot has an eval statement which when given a string will evaluate it. This seems great! Unfortunately, it’s a statement, not a function (so it can’t be used in a function — argv!). The best solution I can come up with is to write a function which returns an expression that can be evaluated using eval. Here goes:

    def exists_func(result,var)=sprintf("%s=exists('%s')?sprintf('%g',var):'NaN'",result,var,var)
    

    Now when you want to use it, you just prefix it with eval

    a=3
    eval exists_func("my_true_result","a")
    print my_true_result #3
    eval exists_func("my_false_result","b")
    print my_false_result #NaN
    

    This goes against the grain a little bit. In most programming languages, you’d probably want to do something like this:

    my_true_result=exists_func(a)
    

    But alas, I can’t figure out how to make that form work.

    Of course, the same thing goes here that always goes with eval. Don’t use this function with untrusted strings.

    *I don’t actually know that it’s impossible, but I’ve never been able to get it to work

    EDIT

    In response to your comment above on the question, I think a function like this would be a little more intuitive:

    def fmt(x)=(x==x)?sprintf("%g",x):"NaN"
    

    With this function, your “sentinal/default” value should be NaN instead of "undefined", but it doesn’t seem like this should make too much of a difference…(Really, if you’re willing to live with "nan" instead of "NaN" you don’t need this function at all — sprintf will do just fine. (Note that this works because according to IEEE, NaN doesn’t equal anything (even itself)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to define a function which accepts an argument
I would like to define a nullary static template member function which would be
I would like to define a function that will be invoked from entry_32.S. It
I would like to define some kind of safe division (and modulo) function, one
I would like to know how to define a returntype in a function in
I would like to define a key & value in Settings.settings and bind the
I would like to define a constant (like the admin-email-adress) depending on the environment.
I would like to define Sorted to be of type ErrorProviderMessageCollection , which is
I would like to define the following in Javascript: var tileXml = new Array();
I would like to define an ordered list with unordered sub-lists. You can see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.