Float and Double in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Float and Double in Perl
# 1  
Old 10-28-2005
Float and Double in Perl

HI!

What is the notation which correspond to C's
Code:
double

and
Code:
float

in Perl??

Please tell me.
Thanks.

Well what I want to do is, for example, calculating
Code:
sqrt(2)

in two way: float and double.
# 2  
Old 10-29-2005
No, Perl stores/processes everything internally as doubles, so there is no distinction between float and double. You can control how many decimal places to display, for instance, with printf()/sprintf(). For instance, running the followings on my Windows system give these:

Code:
D:\Documents and Settings\bernardchan>perl -e "printf('%f', 13/7)"
1.857143
D:\Documents and Settings\bernardchan>perl -e "printf('%0.10f', 13/7)"
1.8571428571

# 3  
Old 10-31-2005
Thank you for your help.
Well I will give it up, and do it with C.
Thanks!
# 4  
Old 03-14-2008
i have problems working with float

id try with

printf('%0.10f',3/2);

and it prints "1.0000000000" instead of "1,5000.."

do i have to use some math perl module??

thanks
# 5  
Old 03-14-2008
Well, on both my Windows and Linux systems "1.5000000000" was printed.

It is possible that the std C library used to compile the perl interpreter may influence that behaviour, but I am not exactly sure.
# 6  
Old 03-14-2008
perl is supposed to interpret numbers as doubles, and you have to force a number to becomes an integer with the int function.

Out of curioisity what does this give?
Code:
printf("%0.10f",3/2);

Changes the ' to " around the format specification...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl double splits

I have a perl script that I was cleaning up, it works very well, but I was wondering if anyone knows of a better way to double split. Sample file: Move_VALIDATE,020212191ABC01,SNSNT---01CAB101A-1-1-4-20 Move_VALIDATE,030323202ABC01,SNSNT01CAB101A-1-1-4-20 Section in script: foreach my... (1 Reply)
Discussion started by: numele
1 Replies

2. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

3. Red Hat

What is float IP?

:confused:We have two servers one active and one stand by as follows Active 202.61.9.9 Stand by 202.61.9.10 Float IP 202.61.9.8 What is use of this float IP? How it is configured? (1 Reply)
Discussion started by: manalisharmabe
1 Replies

4. Shell Programming and Scripting

need a perl replacement for this double for loop

hi everybody, can you help me with this? important: it has to be a pure perl code. zz.lis: accessibility arabic archivers astro audio benchmarks . . . "ls accessibility | grep -v Makefile" outputs accerciser at-poke at-spi at-spi-reference (6 Replies)
Discussion started by: pseudocoder
6 Replies

5. Shell Programming and Scripting

How to substitute the value with in double quotes in perl?

Hi, I have string like this: $str=' DNA OR ("rna AND binding AND protein")'; I just wanted to substitute AND with a blank. How can i do that? I want the output like this: $string= DNA OR ("rna binding protein") (3 Replies)
Discussion started by: vanitham
3 Replies

6. Shell Programming and Scripting

Perl echo with double quotes

I need to echo a string that has double quotes in a Perl script. #!/usr/bin/env perl `echo Rule123 -comment \"blah blah\" >> $filename` I'd like to get below appended to $filename: Rule 123 -comment "blah blah" But instead, the double quotes are lost: Rule 123 -comment blah bah ... (1 Reply)
Discussion started by: slchin
1 Replies

7. Shell Programming and Scripting

problems with double quotes in PERL

I have a cgi script I run through apache2 and I need to have a line that contains double quotes within double quotes. Here's what I need PERL to pass to rrdtool: HRULE:30#BBBB00:"30.0 constant":dashesIt's a little more complicated since I also have variables in the statement which requires... (13 Replies)
Discussion started by: audiophile
13 Replies

8. Shell Programming and Scripting

Displaying double quotes using Perl

Hi Guys, I'm a Perl newbie and was wondering if there's a way of displaying double quotes within double quotes. I'm try to print the contents of the variable to a file by using the system function. Here is an example of my code: #============================== $website = <STDIN>;... (2 Replies)
Discussion started by: kbdesouza
2 Replies

9. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

10. Shell Programming and Scripting

PERL, extract value between double quotes

I know this is probably much simplier than I am making but I need some help please. I have a data file that contains a value on the first line between double quotes ("00043"). I need to assign the value between the first set quotes to a variable in my perl script for comparison analysis. Also,... (6 Replies)
Discussion started by: methos
6 Replies
Login or Register to Ask a Question