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 8693727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:36:38+00:00 2026-06-13T00:36:38+00:00

bc doesn’t like numbers expressed in scientific notation (aka exponential notation). $ echo 3.1e1*2

  • 0

bc doesn’t like numbers expressed in scientific notation (aka exponential notation).

$ echo "3.1e1*2" | bc -l
(standard_in) 1: parse error

but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc to understand exponential notation? If not, what can I do to translate them into a format that bc will understand?

  • 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-13T00:36:40+00:00Added an answer on June 13, 2026 at 12:36 am

    Unfortunately, bc doesn’t support scientific notation.

    However, it can be translated into a format that bc can handle, using extended regex as per POSIX in sed:

    sed -E 's/([+-]?[0-9.]+)[eE]\+?(-?)([0-9]+)/(\1*10^\2\3)/g' <<<"$value"
    

    you can replace the “e” (or “e+”, if the exponent is positive) with “*10^”, which bc will promptly understand. This works even if the exponent is negative or if the number is subsequently multiplied by another power, and allows keeping track of significant digits.

    If you need to stick to basic regex (BRE), then this should be used:

    sed 's/\([+-]\{0,1\}[0-9]*\.\{0,1\}[0-9]\{1,\}\)[eE]+\{0,1\}\(-\{0,1\}\)\([0-9]\{1,\}\)/(\1*10^\2\3)/g' <<<"$value"
    

    From Comments:

    • A simple bash pattern match could not work (thanks @mklement0) as there is no way to match a e+ and keep the – from a e- at the same time.

    • A correctly working perl solution (thanks @mklement0)

      $ perl -pe 's/([-\d.]+)e(?:\+|(-))?(\d+)/($1*10^$2$3)/gi' <<<"$value"
      
    • Thanks to @jwpat7 and @Paul Tomblin for clarifying aspects of sed’s syntax, as well as @isaac and @mklement0 for improving the answer.

    Edit:

    The answer changed quite a bit over the years. The answer above is the latest iteration as of 17th May 2018. Previous attempts reported here were a solution in pure bash (by @ormaaj) and one in sed (by @me), that fail in at least some cases. I’ll keep them here just to make sense of the comments, which contain much nicer explanations of the intricacies of all this than this answer does.

    value=${value/[eE]+*/*10^}  ------> Can not work.
    value=`echo ${value} | sed -e 's/[eE]+*/\\*10\\^/'` ------> Fail in some conditions
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It doesn't look like there's a way to do this in jQuery, but I
Why doesn't this code work? b if b = true Error: undefined local variable
Why doesn't this work? I have bypassed this before but i can't remember how
Doesn't seem to be working for me. I'd open an issue on Github but
Doesn't meter what string I pass to parse I'm always getting Illegal Argument exception
It doesn't seem possible, but I was hoping someone may be aware of a
It doesn't look like TypeConverter is available to use. What is recommended to replace
Doesn't work with other modules, but to give an example. I installed Text::CSV_XS with
Doesn't an ORM usually involve doing something like a select *? If I have
Doesn't value have to return toString() to be able to call value.toString()? When do

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.