LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2020, 03:47 AM   #16
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,658

Rep: Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775Reputation: 4775

Code:
x%100 is equal to x-(int(x/100)*100)
(that's why) post #12 is correct [too]
 
Old 05-25-2020, 06:38 AM   #17
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,752

Rep: Reputation: Disabled
The title of your thread is "How do I round to the nearest hundred", but that's not what you're doing here:
Quote:
Originally Posted by lucmove View Post
Code:
someFunction()   {
	if	(someVar > 2000)	{return 2;}
	if	(someVar > 1900)	{return 1.9;}
	if	(someVar > 1800)	{return 1.8;}
You're always rounding down to the nearest hundred, which is something entirely different.

The pseudo-code for doing this, is as follows:
Code:
someVar = someVar / 100    # the significant part is now whatever's left of the decimal point
someVar = int(someVar)     # discards decimals
someVar = someVar * 100    # restores correct order of magnitude
The expr program, which is part of most Linux distributions, doesn't do floating-point math at all, so this will actually work:
Code:
~$ expr 1243 / 100 \* 100
1200
~$
Note that the asterisk has to be escaped to prevent bash from expanding it.

Most programming languages have an int function, so "someVar = int(someVar/100) * 100" ought to work.

Now that's simply rounding down to the nearest hundred. Since you seem to want the function to return thousands with one decimal place of precision, divide by 10 at the end instead of multiplying by 100. Take care to do so outside the int function, as you don't want the final result rounded or truncated.

Last edited by Ser Olmy; 05-25-2020 at 09:27 AM.
 
Old 05-25-2020, 08:39 AM   #18
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 305

Rep: Reputation: Disabled
Quote:
Originally Posted by lucmove View Post
I have this code:

Code:
someFunction()   {
	if	(someVar > 2000)	{return 2;}
	if	(someVar > 1900)	{return 1.9;}
	if	(someVar > 1800)	{return 1.8;}
	if	(someVar > 1700)	{return 1.7;}
	if	(someVar > 1600)	{return 1.6;}
	if	(someVar > 1500)	{return 1.5;}
	if	(someVar > 1400)	{return 1.4;}
	if	(someVar > 1300)	{return 1.3;}
	if	(someVar > 1200)	{return 1.2;}
	if	(someVar > 1100)	{return 1.1;}
	if	(someVar > 1000)	{return 1;}
	if	(someVar > 900)		{return 0.9;}
	if	(someVar > 800)		{return 0.8;}
	if	(someVar > 700)		{return 0.7;}
	if	(someVar > 600)		{return 0.6;}
	if	(someVar > 500)		{return 0.5;}
	if	(someVar > 400)		{return 0.4;}
	if	(someVar > 300)		{return 0.3;}
	if	(someVar > 200)		{return 0.2;}
	if	(someVar > 100)		{return 0.1;}
}
Of course it's dumb.

It goes up to 2,000 but "someVar" could be a lot larger than that, maybe millions or billions. What is the smart way to achieve what I want?

Please assume some kind of shell scripting or pseudo code.

TIA
I think your specification needs to be more specific. I read this as "divide the input by 1000 and truncate to a single decimal point.

In this case In javascript:
Code:
var f1 = function (x) { return Math.trunc (x / 100) / 10; };
var f2 = function (x) { return Math.floor (x / 100) / 10; };
 
Old 05-26-2020, 12:08 AM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,056

Rep: Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469
Let's note that 0.1 cannot be precisely represented as binary floating number, so 0.1*10 != 1 (but near enough).
Usually it doesn't matter, except when it does.
 
Old 05-26-2020, 01:36 AM   #20
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,605

Rep: Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279
Quote:
Originally Posted by lucmove View Post
That doesn't work.

900/1000 = 0.9. OK
956/1000 = 0.956. Wrong. I need 0.9.
9560/1000 = 9.56. Wrong. I need 9.5.

Any shell language or pseudo code will do.
I think you're going to want to play with bc and its scale argument.

Code:
bash-3.2$ echo 'scale=1;956 / 1000' | bc -l
.9
bash-3.2$ echo 'scale=2;956 / 1000' | bc -l
.95
bash-3.2$ echo 'scale=3;956 / 1000' | bc -l
.956

Last edited by dugan; 05-26-2020 at 01:48 AM.
 
Old 06-10-2020, 11:33 AM   #21
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 906

Rep: Reputation: 210Reputation: 210Reputation: 210
...

Last edited by AnanthaP; 06-11-2020 at 04:21 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Assitence wiht rounding to nearest inch Timberwolf753 Programming 6 10-12-2006 06:14 PM
umask taking the nearest even number anjanesh Linux - General 4 07-17-2006 12:12 PM
PS2 mouse goes crazy [it goes round n round n round...] goci Linux - Hardware 2 10-09-2003 08:15 AM
STUPID question on routing and my nearest computer kngharv Linux - Networking 1 07-12-2002 05:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration