Sponsored Content
Top Forums Shell Programming and Scripting how can i find sqrt of a any number without using expr Post 302307336 by otheus on Wednesday 15th of April 2009 06:15:26 AM
Old 04-15-2009
Heh, That expression is very problematic. Here's one that works. The first argument is a number, which becomes 0 if it's not provided or if it is less than 0. The second argument is the number of decimal places to output -- or 4 if not provided.
Code:
y=$1
scale=4
# Make sure input is a valid number.
test -n "$y" || y=0; test "$y" -ge 0 || y=0
# Make sure scale is some number
test -z "$2" || scale=$2
# calc square root via bc
{ echo "scale=$scale"; echo "sqrt($y)" ; } | bc

 

10 More Discussions You Might Find Interesting

1. Programming

sqrt

Hi! when i'm trying to compile this lite example on my linux machine I'll get errors and i don't know why.. #include <stdio.h> #include <math.h> /* needed by sqrt() */ int main() { printf("%f", sqrt(10.0)); return (0); } this is the error: /tmp/cc33hNVHK.o: In function... (1 Reply)
Discussion started by: CreamHarry
1 Replies

2. Programming

sqrt is not find???

I was writing a simple program in linux, which includes sqrt function of c. I included the math.h. But when I use gcc to compile it, it gave an error message: /home/murat/tmp/ccOv9upo.o(.text+0x4b): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status I... (2 Replies)
Discussion started by: murataht
2 Replies

3. Shell Programming and Scripting

How to create SQRT function in catenate file

HI, I have a file which i catenate and using the fields in the file, I would like to get sqrt of it. I tried to man the function but it normally would need an echo as well as bc. What I am intending to find out is catenate a file where let say cat a.txt| awk ' { t= h*($3+$2); t=... (7 Replies)
Discussion started by: ahjiefreak
7 Replies

4. Shell Programming and Scripting

Find first digit in string using expr index

I have looked for hours for an answer, so I have decided to request your guidance. I want to substract the first number (series of digits) contained in a string. This string is the output of another command. The substring (number) can be located at any position inside the string. I want to... (4 Replies)
Discussion started by: jcd
4 Replies

5. Shell Programming and Scripting

PERL: Simple reg expr validate 6 digits number

Hi there! I'm trying to validate a simple 6 digits number with reg expr. I ONLY want 6 digits so when i type 7 digits the script should no validate the number. I've write this code: #!/usr/bin/perl while(<STDIN>){ if($_=~/\d{6}/){ print "Bingo!\n"; ... (2 Replies)
Discussion started by: BufferExploder
2 Replies

6. Programming

C Library reference error using sqrt() - will not compile.

This so basic that it should work.... Any ideas would be appreciared. Using a number directly in the sqrt allows it to compile. primrose > cat a.c #include <stdio.h> #include <math.h> int main(void) { double abcd=9; printf("%f\n",sqrt(abcd)); } primrose > gcc a.c Undefined first... (2 Replies)
Discussion started by: plastichead
2 Replies

7. UNIX for Dummies Questions & Answers

sqrt in bash

Hi, i have a the following script: #!/bin/bash a=3 b=9 let "c= b*a" let "d=sqrt $c " echo $d But when i execute the code, it gives me the an error saying: line 5: let: d=sqrt 27 : syntax error in expression (error token is "27 ") Can any body tell me what I'm doing wrong? (5 Replies)
Discussion started by: limadario
5 Replies

8. Shell Programming and Scripting

Find command - using regular expr

Hi, I would like to use find command to find file with a predefined extension for example find . modules/*.ksh *.lib I thought it's possible to use something like : find . modules/*. but it does not work. is there any other way? Thanks you (2 Replies)
Discussion started by: zam
2 Replies

9. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

10. Shell Programming and Scripting

Q: Is SQRT(n) possible in a POSIX compliant shell? A: Yes within limits.

Hi all... This is just a fun project to see if it is possible to get a square root of a positive integer from 1 to 9200000 to 6 decimal places on a 64 bit architecture machine. It is coded around dash and the results show the values from 0 to 10000. Complex numbers can easily be catered for by... (3 Replies)
Discussion started by: wisecracker
3 Replies
Gtk2::Scale(3)						User Contributed Perl Documentation					    Gtk2::Scale(3)

NAME
Gtk2::Scale HIERARCHY
Glib::Object +----Glib::InitiallyUnowned +----Gtk2::Object +----Gtk2::Widget +----Gtk2::Range +----Gtk2::Scale INTERFACES
Glib::Object::_Unregistered::AtkImplementorIface Gtk2::Buildable Gtk2::Orientable METHODS
integer = $scale->get_digits $scale->set_digits ($digits) o $digits (integer) boolean = $scale->get_draw_value $scale->set_draw_value ($draw_value) o $draw_value (boolean) layout = $scale->get_layout Since: gtk+ 2.4 (x, y) = $scale->get_layout_offsets Since: gtk+ 2.4 positiontype = $scale->get_value_pos $scale->set_value_pos ($pos) o $pos (Gtk2::PositionType) PROPERTIES
'digits' (integer : readable / writable / private) The number of decimal places that are displayed in the value 'draw-value' (boolean : readable / writable / private) Whether the current value is displayed as a string next to the slider 'value-pos' (Gtk2::PositionType : readable / writable / private) The position in which the current value is displayed SIGNALS
string = format-value (Gtk2::Scale, double) ENUMS AND FLAGS
enum Gtk2::PositionType o 'left' / 'GTK_POS_LEFT' o 'right' / 'GTK_POS_RIGHT' o 'top' / 'GTK_POS_TOP' o 'bottom' / 'GTK_POS_BOTTOM' SEE ALSO
Gtk2, Glib::Object, Glib::InitiallyUnowned, Gtk2::Object, Gtk2::Widget, Gtk2::Range COPYRIGHT
Copyright (C) 2003-2008 by the gtk2-perl team. This software is licensed under the LGPL. See Gtk2 for a full notice. perl v5.12.1 2010-07-05 Gtk2::Scale(3)
All times are GMT -4. The time now is 07:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy