Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 08-25-2019, 02:38 PM   #1
China Diapers
Member
China Diapers began at the beginning.
 
Posts: 21
Karma: 10
Join Date: May 2011
Device: PRS-600
Help with test template formula

I have created the below template:

program:format_number(test(field('#duration'),divi de(field('#page_count'),field('#duration')),0),"{0 :5.1f}");

What I was intending it would do is return 0 where the 'duration' field is blank (thereby avoiding division by zero) else the quotient of page count divided by duration value.

What is actually happening is the result is never 0, it is either the quotient or a division by zero error, so it appears the test is ignored or I am misunderstanding the purpose of the test function.

Screenshot attached for context.

Any ideas?
Attached Thumbnails
Click image for larger version

Name:	Screenshot_2019-08-25_19-32-14.png
Views:	160
Size:	17.2 KB
ID:	173120  

Last edited by China Diapers; 08-25-2019 at 03:23 PM.
China Diapers is offline   Reply With Quote
Old 08-25-2019, 03:41 PM   #2
China Diapers
Member
China Diapers began at the beginning.
 
Posts: 21
Karma: 10
Join Date: May 2011
Device: PRS-600
Quote:
Originally Posted by China Diapers View Post
I have created the below template:

program:format_number(test(field('#duration'),divi de(field('#page_count'),field('#duration')),0),"{0 :5.1f}");

What I was intending it would do is return 0 where the 'duration' field is blank (thereby avoiding division by zero) else the quotient of page count divided by duration value.

What is actually happening is the result is never 0, it is either the quotient or a division by zero error, so it appears the test is ignored or I am misunderstanding the purpose of the test function.

Screenshot attached for context.

Any ideas?
That said, the below returns true where the duration value is populated, and false where it is not as I would expect.

program:test(field('#duration'),"True","False");
China Diapers is offline   Reply With Quote
Advert
Old 08-25-2019, 03:55 PM   #3
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,412
Karma: 8012664
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Your problem comes from the fact that arguments to template functions are all evaluated before the template function itself. This means that the divide is calculated even when the test is false. Said another way, the test is an "&" and not an "&&". There is no shortcutting.

You can fix it by computing a legal divisor that is used in the test, but with the test returning the "else" value. This template is an example. I didn't use your column names, but it should be clear enough.
program:
Code:
program:
	dividend = raw_field('#myint2');
	divisor = ifempty(field('#myint'), 1);
	format_number(
		test(field('#myint'),
			divide(dividend, divisor),
			0),
		"{0:5.1f}");
The reason I used "raw_field" is that my column was formatted with currency signs and commas, and I wanted to retrieve only the base number.
chaley is offline   Reply With Quote
Old 08-25-2019, 05:24 PM   #4
China Diapers
Member
China Diapers began at the beginning.
 
Posts: 21
Karma: 10
Join Date: May 2011
Device: PRS-600
Quote:
Originally Posted by chaley View Post
Your problem comes from the fact that arguments to template functions are all evaluated before the template function itself. This means that the divide is calculated even when the test is false. Said another way, the test is an "&" and not an "&&". There is no shortcutting.

You can fix it by computing a legal divisor that is used in the test, but with the test returning the "else" value. This template is an example. I didn't use your column names, but it should be clear enough.
program:
Code:
program:
	dividend = raw_field('#myint2');
	divisor = ifempty(field('#myint'), 1);
	format_number(
		test(field('#myint'),
			divide(dividend, divisor),
			0),
		"{0:5.1f}");
The reason I used "raw_field" is that my column was formatted with currency signs and commas, and I wanted to retrieve only the base number.
Thanks for the explanation. Your example works great.
China Diapers is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using built-in template functions in a custom template function ilovejedd Library Management 4 01-28-2018 12:20 PM
There *IS* a formula! cromag Lounge 5 01-10-2015 10:21 PM
Troubleshooting Kindle and math formula DrShakalu Amazon Kindle 12 12-11-2011 07:25 AM
Formula Plots PuxyYunm General Discussions 9 05-15-2011 04:19 AM


All times are GMT -4. The time now is 04:28 PM.


MobileRead.com is a privately owned, operated and funded community.