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

  • Home
  • SEARCH
  • 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 9214735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:02:16+00:00 2026-06-18T02:02:16+00:00

I get six different results when dividing by zero in mawk: $ echo |

  • 0

I get six different results when dividing by zero in mawk:

$ echo | awk '{print -1/0 }' ; echo $?
-inf
0
$ echo | awk '{print 0/0 }' ; echo $?
-nan
0
$ echo | awk '{print 1/0 }' ; echo $?
inf
0
$ echo | awk '{printf ("%i\n", -1/0) }' ; echo $?
-2147483647
0
$ echo | awk '{printf ("%i\n", 0/0) }' ; echo $?
-2147483647
0
$ echo | awk '{printf ("%i\n", 1/0) }' ; echo $?
2147483647
0

I get a “success” exit code for each case.

  1. Why does this happen?
  2. What can I do about it? Do I always have to check for zero divisors before doing a division or is there a way I can rely on awk’s error handling and exit codes?
  • 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-18T02:02:18+00:00Added an answer on June 18, 2026 at 2:02 am

    I’m guessing you are using mawk, none of awk, nawk or gawk have this behaviour, division by zero is not supported on those.

    Your answers are +inf for positive infinity, nan for “not a number” and -inf for negative infinity, all as expected. These are only output when printing a float, i.e. with %.6g which is the default OFMT (default format used for printing numbers).

    $ mawk 'BEGIN {printf ("%i\n", 1/0) }'
    2147483647
    $ mawk 'BEGIN {printf ("%f\n", 1/0) }'
    inf
    $ gawk 'BEGIN {printf ("%f\n", 1/0) }'
    gawk: fatal: division by zero attempted
    

    When you print explicitly with “%i” you get +HUGE or -HUGE instead, this is converted to an int (signed 32-bit in your case) and printed as + or – (2^31-1).

    The usual practice is to always check for divide by zero, ideally minimising the number of times you need to check by reorganising your expressions — divide by zero causes other implementations of awk to simply terminate.

    When mawk is building it detects the capabilities of your C math library:

    $ ./configure
    [...]
    checking handling of floating point exceptions
        division by zero does not generate an exception
        overflow does not generate an exception
        math library supports ieee754
    

    You can get “standard” behaviour if you build with NOINFO_SIGFPE defined:

    $ ./configure CFLAGS="-DNOINFO_SIGFPE"
    $ make clean && make
    $ ./mawk 'BEGIN {printf ("%f\n", 1/0) }'
    mawk: run time error: division by zero
        FILENAME="" FNR=0 NR=0
    

    (though this isn’t documented, it may not be something you should rely on).

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

Sidebar

Related Questions

We deploy our .net web application in six server(each with one copy) to get
I get the following error even when my JAVA_HOME is set correctly. C:\workspace-sts-2.8.0.RELEASE\JBClient\target>echo %JAVA_HOME%
In one of my Activities I do have up to six different AsyncTasks that
I want to get the six week sundays weekno using the SQL Server 2008.For
I want to get a six digit number by user and spit it into
I have six imageViews in my gridview. I am trying to get the focus
I added six data to sorted set. Then I use zinterstore to get expected
I need to get two summations in the same query based on different criteria.
I'm trying to get the month, six months out from the current date. I've
Get class from div inside an li and add to the same li. The

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.