Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Drowning w/ Floating Points

Original Message
Name: arnettme
Date: June 5, 2007 at 08:42:20 Pacific
Subject: Drowning w/ Floating Points
OS: Solaris 9
CPU/Ram: 1024
Model/Manufacturer: Sun Blade 150
Comment:
I wrote a simple ksh script that’s gone bad. At the end of a quiz the script calculates the Marines quiz scores, with the following:

((a=100 / $b)) # $b equals # of questions.
((c=100 - ($a * $e))) # $e equals # of missed
# questions.

All is well as long as the number of quiz questions ($b) isn't greater than 100.

Unfortunate for me, but great for the Marines, there are 3 quizzes that are greater than 150 questions and when the Marines run those quizzes, the value of ($b=.66) is rounded to 0 and they all make 100's regradless of the number of wrong answers.

Does any one have a fix? Oh, one last thing, I am stuck with the shells that come bundled with Solaris 9.

mike


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: June 5, 2007 at 09:09:24 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
If you are using #!/bin/ksh, that is the '88 version of the Korn shell, and floats are not supported. They are not supported until the '93 version. On Solaris 9, the desk top version, dtksh, is 93 compliant:

#!/usr/dt/bin/dtksh

Use it, and it should work for you.


Report Offensive Follow Up For Removal

Response Number 2
Name: arnettme
Date: June 5, 2007 at 11:22:16 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
Nails, thanks for the info on dtksh, but with the above script, my problem continues. Maybe it's the way I put it on the command line. i.e.,

# /usr/dt/bin/dtksh
# b=100
# c=151
# ((a=$b / $c))
# print $a
0
#

"a" should return a vaule of .66.

mike


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: June 5, 2007 at 12:24:27 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
#!/usr/dt/bin/dtksh

The dtksh is brain dead as it doesn't know you are working with floats. I had to append a .0 in order to get this to work.

b=100.0
c=151.0

f=$(($b / $c))
print $f

One way of forcing your integer to a float is to use the external printf comand to format it:

d=100
e=151
d=$(printf "%4.1f" $d)
e=$(printf "%4.1f" $e)
g=$(($d / $e))
print $g

Have you considered using something other than the shell for your float arithmetic - such as awk or bc?

Here is an example using bc:

m=100
n=151
v=$(bc << MSG
scale=2
$m / $n
MSG
)
echo $v


Report Offensive Follow Up For Removal

Response Number 4
Name: arnettme
Date: June 5, 2007 at 17:02:37 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
Nails, that worked great! I used the dtsk example. One last request if I might. Is there a way of having the return be only two space holders after the decimal point? i.e.,

From

92.3759736192

to just

92.37

Thanks for your help.

mike


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: June 5, 2007 at 17:55:40 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
If you use the bc command to perform the arithmetic, you can use the scale command as I did in the above example. If you don't, the printf command can be used to format a number:

x=92.3759736192

x=$(printf "%3.2f" $x)


Report Offensive Follow Up For Removal


Response Number 6
Name: arnettme
Date: June 7, 2007 at 07:40:55 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
Nails thank you very much, that worked great!
In this world of knowledge is power, its refreshing to know that some people are still willing to share they're intellect.

mike


Report Offensive Follow Up For Removal

Response Number 7
Name: nails
Date: June 7, 2007 at 12:20:29 Pacific
Subject: Drowning w/ Floating Points
Reply: (edit)
Mike:

Thank you for the compliment, but it's more experience than intellect. I'm just replying to the kindness paid me. I believe the young people call it "paying it forward".

Nails


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Drowning w/ Floating Points

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC